Programming Articles - Page 1199 of 3363

Show decimal places and scientific notation on the axis of a Matplotlib plot

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:06:19

7K+ Views

To show decimal places and scientific notation on the axis of a matplotlib, we can use scalar formatter by overriding _set_format() method.StepsCreate x and y data points using numpy.Plot x and y using plot() method.Using gca() method, get the current axis.Instantiate the format tick values as a number class, i.e., ScalarFormatter.Set size thresholds for scientific notation, using set_powerlimits((0, 0)) method.Using set_major_formatter() method, set the formatter of the major ticker.To display the figure, use show() method.Exampleimport numpy as np from matplotlib.ticker import ScalarFormatter from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True class ScalarFormatterClass(ScalarFormatter):    def _set_format(self):       ... Read More

How to plot vectors in Python using Matplotlib?

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

8K+ Views

To plot vectors in Python using matplotlib, we can take the following steps −Create a matrix of 2×3 dimension.Create an origin point, from where vecors could be originated.Plot a 3D fields of arrows using quiver() method with origin, data, colors and scale=15.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.array([[2, 1], [-1, 2], [4, -1]]) origin = np.array([[0, 0, 0], [0, 0, 0]]) plt.quiver(*origin, data[:, 0], data[:, 1], color=['black', 'red', 'green'], scale=15) plt.show()Output

How to rotate the rectangle patch in a plot using Matplotlib?

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

2K+ Views

To rotate the rectangle patch in a plot, we can use angle in the Rectangle() class to rotate it.StepsCreate a figure and a set of subplots using subplots() method.Add a rectangle on the patch, angle=45°.Add a patch on the axis.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 figure, ax = plt.subplots(1) rectangle = patches.Rectangle((0.4, 0.25), 0.5, 0.5, edgecolor='orange', facecolor="green", linewidth=2, angle=45) ax.add_patch(rectangle) plt.show()Output

How to change the spacing between ticks in Matplotlib?

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

8K+ 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

18K+ 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

5K+ 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

1K+ 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

2K+ 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

Advertisements