Make Two Histograms Have the Same Bin Width in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:20:36

2K+ Views

To make two histograms having same bin width, we can compute the histogram of a set of data.StepsCreate random data, a, and normal distribution, b.Initialize a variable, bins, for the same bin width.Plot a and bins using hist() method.Plot b and bins using hist() method.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 = np.random.random(100) * 0.5 b = 1 - np.random.normal(size=100) * 0.1 bins = 10 bins = np.histogram(np.hstack((a, b)), bins=bins)[1] plt.hist(a, bins, edgecolor='black') plt.hist(b, bins, edgecolor='black') plt.show()OutputRead More

Increase/Reduce Font Size of X and Y Tick Labels in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:16:36

3K+ Views

To increase/reduce the fontsize of x and y tick labels in matplotlib, we can initialize the fontsize variable to reduce or increase font size.StepsCreate a list of numbers (x) that can be used to tick the axes.Get the axis using subplot() that helps to add a subplot to the current figure.Set ticks on x and y axes using set_xticks and set_yticks methods respectively and list x (from step 1).Set tick labels with label lists (["one", "two", "three", "four"]) using set_xticklabels() and set_yticklabels() with fontsize variable.To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to ... Read More

Plot a Rectangle Inside a Circle in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:12:51

770 Views

To plot a rectangle inside 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 to the current axis.Make a rectangle and a circle instance using Rectangle() and Circle() class.Add a patch on the axes.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from 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(111) rect1 = patches.Rectangle((-2, -2), 4, 2, color='yellow') circle1 = matplotlib.patches.Circle((0, 0), radius=3, color='red') ax.add_patch(circle1) ax.add_patch(rect1) plt.xlim([-5, 5]) plt.ylim([-5, 5]) plt.axis('equal') plt.show()OutputRead More

Difference Between set_xlim and set_xbound in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:09:52

536 Views

set_xlim − Set the X-axis view limits.set_xbound − Set the lower and upper numerical bounds of the X-axis.To set the xlim and xbound, we can take the following steps −Using subplots(2), we can create a figure and a set of subplots. Here, we are creating 2 subplots.Create x and y data points using numpy.Use axis 1 to plot x and y data points using plot() method.Set x limit using set_xlim() method.Use axis 2 to plot x and y data points using plot() method.Sex xbound using set_xbound() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Animate a Pcolormesh in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:05:57

3K+ Views

To animate pcolormesh in matplotlib, we can take the following steps −Create a figure and a set of subplots.Create x, y and t data points using numpy.Create X3, Y3 and T3, return coordinate matrices from coordinate vectors using meshgrid.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.Make a colorbar with colormesh axis.Animate pcolormesh using Animation() class method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-3, 3, 91) t = np.linspace(0, 25, 30) y = np.linspace(-3, 3, 91) X3, Y3, T3 = ... Read More

Add Colorbar for a Hist2D Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:01:58

4K+ Views

To add a colorbar for hist2d plot, we can pass a scalar mappable object to colorbar() method's argument.StepsCreate x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Make a 2D histogram plot using hist2d() method.Create a colorbar for a hist2d scalar mappable instance.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, colors plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(100) y = np.random.randn(100) + 5 fig, ax = plt.subplots() hh = ax.hist2d(x, y, bins=40, norm=colors.LogNorm()) fig.colorbar(hh[3], ax=ax) plt.show()OutputRead More

Set Axis Ticks in Multiples of Pi in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:44:15

5K+ Views

To set axis ticks in multiples of pi in Python, we take following steps −Initialize a pi variable, create theta and y data points using numpy.Plot theta and y using plot() method.Get or set the current tick locations and labels of the X-axis using xticks() method.Convenience method to set or retrieve autoscaling margins using margins() method.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 pi = np.pi theta = np.arange(-2 * pi, 2 * pi+pi/2, step=(pi / 2)) y = np.sin(theta) plt.plot(theta, y) plt.xticks(theta, ['-2π', '-3π/2', 'π', ... Read More

Make Hollow Square Marks with Matplotlib in Python

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:40:16

6K+ Views

To make hollow square marks with matplotlib, we can use marker 'ks', markerfacecolor='none', markersize=15, and markeredgecolor=red.StepsCreat x and y data points using numpy.Create a figure or activate an existing figure, add an axes to the figure as part of a subplot arrangement.Plot x and y data points using plot() method. To make hollow square marks, we can use marker "ks" and markerfacecolor="none", markersize="15" and markeredge color="red".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 x = np.linspace(-2, 2, 10) y = np.sin(x) fig = plt.figure() ax1 = ... Read More

Display All Label Values in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:37:08

7K+ Views

To display all label values, we can use set_xticklabels() and set_yticklabels() methods.StepsCreate a list of numbers (x) that can be used to tick the axes.Get the axis using subplot() that helps to add a subplot to the current figure.Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).Set tick labels with label lists (["one", "two", "three", "four"]) and rotation of 45 using set_xticklabels() and set_yticklabels().To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to add space. Argument direction (in) helps to put ticks inside ... Read More

Place a Table on a Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:31:31

815 Views

StepsUsing the subplots() method, create a figure and a set of subplots with figure size (7, 7).Create a data frame with two keys, time and speed.Get the size of the array.Add a table to the current axis using the table method.Shrink the font size until the text fits into the cell width.Set the font size in the table.Set the face color, edge color,  and text color by iterating the matplotlib table.Save and display the figure.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), ... Read More

Advertisements