Found 1034 Articles for Matplotlib

How to change the spacing between ticks in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:56:03

7K+ Views

To set ticks on a fixed position or change the spacing between ticks in matplotlib, we can take the following steps −Create a figure and add a set of subplots.To set the ticks on a fixed position, create two lists with some values.Use set_yticks and set_xticks methods to set the ticks on the axes.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() xtick_loc = [0.20, 0.75, 0.30] ytick_loc = [0.12, 0.80, 0.76] ax.set_xticks(xtick_loc) ax.set_yticks(ytick_loc) plt.show()OutputRead More

How to normalize a histogram in Python?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:55:40

14K+ Views

To normalize a histogram in Python, we can use hist() method. In normalized bar, the area underneath the plot should be 1.StepsMake a list of numbers.Plot a histogram with density=True.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True k = [5, 5, 5, 5] x, bins, p = plt.hist(k, density=True) plt.show()Output

How to set a default colormap in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:55:19

4K+ Views

To set a default colormap in matplotlib, we can take the following steps −Create random data using numpy, array dimension 4×4.Create two axes and one figure using subplots() method.Display the data as an image with the default colormap.Set the title of the image, for the default colormap.Set the default colormap using matplotlib rcParams.Display the data as an image, with set default colormap.Set the title of the image, for the default colormap.Adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import matplotlib as mpl plt.rcParams["figure.figsize"] = [7.00, 3.50] ... Read More

How to plot a 3D density map in Python with Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:54:49

2K+ Views

To plot a 3D density map in Python with matplotlib, we can take the following steps −Create side, x, y and z using numpy. Numpy linspace helps to create data between two points based on a third number.Return the coordinate matrices from coordinate vectors using side data.Create exponential data using x and y (Step 2).Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, cm, colors import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True side = np.linspace(-2, 2, 15) X, Y = np.meshgrid(side, side) Z = ... Read More

Add minor gridlines to Matplotlib plot using Seaborn

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:54:25

3K+ Views

To add minor gridlines to matplotlib plot using Seaborn, we can take the following steps −Create a list of numbers to plot a histogram using Seaborn.Plot univariate or bivariate histograms to show distributions of datasets using histplot() method.To make minor grid lines, we can first use major grid lines and then minor grid lines.To display the figure, use show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [5, 6, 7, 2, 3, 4, 1, 8, 2] ax = sns.histplot(x, kde=True, color='red') ax.grid(b=True, which='major', color='black', linewidth=0.075) ax.grid(b=True, which='minor', color='black', linewidth=0.075) plt.show()OutputRead More

How to annotate the points on a scatter plot with automatically placed arrows in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:53:59

976 Views

To annotate the point on a scatter plot with automatically placed arrows, we can take the following steps −Create points for x and y using numpy.Create labels using xpoints.Use scatter() method to scatter the points.Iterate labels, xpoints and ypoints and annotate plot with label, x and y with different properties.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xpoints = np.linspace(1, 10, 25) ypoints = np.random.rand(25) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints):    plt.annotate(   ... Read More

Setting a relative frequency in a Matplotlib histogram

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:53:34

1K+ Views

To set a relative frequency in a matplotlib histogram, we can take the following steps −Create a list of numbers for data and bins.Compute the histogram of a set of data, using histogram() method.Get the hist and edges from the histogram.Find the frequency of the histogram.Make a bar with bins (step 1) and freq data (step 4).To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True a = [-0.125, .15, 8.75, 72.5, -44.245, 88.45] bins = np.arange(-180, 181, 20) hist, edges = np.histogram(a, bins) freq = hist/float(hist.sum()) plt.bar(bins[:-1], ... Read More

How to plot a circle in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:53:08

13K+ Views

To plot a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot arrangement to the current axis.Create a true circle at a center using Circle class.Add a patch to the current axis.Set limits of the x and y axes.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot() circle1 = patches.Circle((0.2, 0.2), radius=0.5, color='green') ax.add_patch(circle1) ax.axis('equal') plt.show()OutputRead More

Add alpha to an existing Matplotlib colormap

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:42:22

2K+ Views

To add apha to an existing matplotlib colormap, we can take the following steps −Create data with a 4×4 dimension array using numpy.Get the colormap using plt.cm.RdBU.Create a new colormap using numpy.Set alpha value to the new colormap.Generate a colormap object using the list of colors.Create a new figure or activate an existing figure using figure() method.Add a subplot to the current figure, nrows=1, ncols=2 at index=1.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.Create a colorbar for scalar mappable instance.Repeat steps 7 to 9, at index 2.Use tight_layout() to adjust the padding between and around the subplots.To ... Read More

How to annotate a heatmap with text in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:40:15

1K+ Views

To annotate a heatmap with text in matplotlib, we can take the following steps −Create random data with 4×4 dimension array.Create a pseudocolor plot with a non-regular rectangular grid, using pcolor() method.To put text in the pixels, we can use text() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) heatmap = plt.pcolor(data, cmap="PuBuGn_r") for y in range(data.shape[0]):    for x in range(data.shape[1]):       plt.text(x + 0.5, y + 0.5, '%.4f' % data[y, x],          horizontalalignment='center',         ... Read More

Advertisements