Found 784 Articles for Data Visualization

How to plot arbitrary markers on a Pandas data series using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 11:46:37

305 Views

To plot arbitrary markers on a Pandas data series, we can use pyplot.plot() with markers.StepsSet the figure size and adjust the padding between and around the subplots.Make a Pandas data series with axis labels (including timeseries).Plot the series index using plot() method with linestyle="dotted".Use tick_params() method to rotate overlapping labels.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ts = pd.Series(np.random.randn(10), index=pd.date_range('2021-04-10', periods=10)) plt.plot(ts.index, ts, '*', ls='dotted', color='red') plt.tick_params(rotation=45) plt.show()OutputRead More

How to change the range of the X-axis and Y-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 02-Sep-2023 10:31:28

74K+ Views

To change the range of X and Y axes, we can use xlim() and ylim() methods.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using plot() method.Set the X and Y axes limit.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-15, 15, 100) y = np.sin(x) plt.plot(x, y) plt.xlim(-10, 10) plt.ylim(-1, 1) plt.show()Output

How to view all colormaps available in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Jun-2021 12:06:49

140 Views

To view all colormaps available in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangementMake an axis that is divider on the existing axes.Create random data using numpy.Display the data as an image, i.e., on a 2D regular raster.Create a colorbar for a ScalarMappable instance, im.Set a title for the current figure.Animate the image with all colormaps available in matplotlib.Make an animation by repeatedly calling a function.To display the figure, ... Read More

How to customize X-axis ticks in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:04:07

4K+ Views

To customize X-axis ticks in Matplotlib, we can change the ticks length and width.StepsSet the figure size and adjust the padding between and around the subplots.Create lists for height, bars and y_pos data points.Make a bar plot using bar() method.To customize X-axis ticks, we can use tick_params() method, with color=red, direction=outward, length=7, and width=2.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True height = [3, 12, 5, 18, 45] bars = ('A', 'B', 'C', 'D', 'E') y_pos = np.arange(len(bars)) plt.bar(y_pos, height, color='yellow') plt.tick_params(axis='x', colors='red', direction='out', ... Read More

How to remove grid lines from an image in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:11:11

6K+ Views

To remove grid lines from an image, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Load an image from a file.Convert the image from one color space to another.To remove grid lines, use ax.grid(False).Display the data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import cv2 plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True img = cv2.imread('bird.jpg') img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.grid(False) plt.imshow(img) plt.show()OutputRead More

How to save a plot in Seaborn with Python (Matplotlib)?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:11:59

4K+ Views

To save a plot in Seaborn, we can use the savefig() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot pairwise relationships in a dataset.Save the plot into a file using savefig() method.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"]) sns_pp = sns.pairplot(df) sns_pp.savefig("sns-heatmap.png")OutputWhen we execute the code, it will create the following plot and save it ... Read More

How to customize the X-axis in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:10:45

3K+ Views

To customize the X-axis label, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, to get the number of sample data.Create x and y data points using numpyPlot x and y data points using plot() method.Customize the X-axis labels with fontweight, color, fontsize, and alignment.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True N = 100 x = np.random.rand(N) y = np.random.rand(N) plt.plot(x, y, 'r*') plt.xlabel('X-axis Label', fontweight='bold', color='orange', ... Read More

How to align axis label to the right or top in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:01:43

2K+ Views

To align axis label to the right (X-axis label) or top (Y-axis label), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Initialize a variable, N, for number data samples.Plot x and y data points using plot() method.Set xlabel and ylabel at the right and top locations, respectively.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() N = 10 x = np.random.rand(N) y = ... Read More

Create a legend with Pandas and Matplotlib.pyplot

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:00:33

835 Views

To create a legend with Pandas and matplotib.pyplot(), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.Plot the dataframe instance with bar class by name and legend is True.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame({'Numbers': [3, 4, 1, 7, 8, 5], 'Frequency': [2, 4, 1, 4, 3, 2]}) df.plot(ax=ax, kind='bar', legend=True) plt.show()Output

Frequency plot in Python/Pandas DataFrame using Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Jun-2021 12:00:11

13K+ Views

To show a frequency plot in Python/Pandas dataframe using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.Return a Series containing the counts of unique values.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame({'numbers': [2, 4, 1, 4, 3, 2, 1, 3, 2, 4]}) df['numbers'].value_counts().plot(ax=ax, kind='bar', xlabel='numbers', ylabel='frequency') plt.show()OutputRead More

Advertisements