Plot Bar Chart with Multiple Labels in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 12:21:31

3K+ Views

To plot a bar chart with multiple labels in Matplotlib, we can take the following steps −Make some data set for men_means, men_std, women_means, and women_std.Make index data points using numpy.Initialize the width of the bars.Use subplots() method to create a figure and a set of subplots.Create rects1 and rects2 bars rectangle using bar() method.Use set_ylabel(), set_title(), set_xticks() and set_xticklabels() methods.Place a legend on the plot.Add multiple labels for bar chart using autolabel() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True men_means, men_std = (20, ... Read More

Rotating Axes Label Text in 3D Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 12:17:23

4K+ Views

To rotate axes label text in 3D matplotlib, we can use set_zlabel() method with rotation in the method's argument.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Add a subplot to the current axis with projection="3d".Initialize a variable, angle, for an angle.Set Z-axis label using set_zlabel() method with a rotation.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') angle = 45 ax.set_zlabel('Z-Axis', rotation=angle) plt.show()OutputRead More

Drawing Multiple Legends on the Same Axes in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 10:13:58

10K+ Views

To draw multiple legends on the same axes in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplotsPlot lines using two lists with different labels, linewidth and linestyle.Place the first legend at the upper-right location.Add artist, i.e., first legend on the current axis.Place the second legend on the current axis at the lower-right location.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 2, 3], label="Line 1", linestyle='--') line2, = plt.plot([3, 2, 1], label="Line 2", ... Read More

Best Way to Plot an Angle Between Two Lines in Matplotlib

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

2K+ Views

The best way to plot an angle between two lines in Matplotlib is to use the Arc class to make an angle arc to plot the angle in between.StepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.Create 2D line instances as l1 and l2.Add lines to the current axes.To plot an angle, call a user-defined method that returns an elliptical arc. Arc length could be created using slopes of the lines..Add an ... Read More

Display Percentage Above a Bar Chart in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 10:07:46

3K+ Views

To display percentage above a bar chart in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points; initialize a variable, width.Create a figure and a set of subplots using subplots() method.Add bars with x and y data points.Iterate bars patches; put text over the bars using text() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4, 5] y = [3, 4, 2, ... Read More

Make Two Markers Share the Same Label in Matplotlib Legend

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 10:05:25

1K+ Views

To make two markers share the same label in the legend using Matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y(as a sin(x) and cos(x)), using plot() method.Place legend with location=1.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(-5, 5, 100) plt.plot(x, np.sin(x), ls="dotted", label='y=f(x)') plt.plot(x, np.cos(x), ls="-", label='y=f(x)') plt.legend(loc=1) plt.show()OutputIt is not recommended to make two markers share the same label ... Read More

Manipulation on Horizontal Space in Matplotlib Subplots

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 10:03:23

1K+ Views

To manipulate on horizontal space in Matplotlib subplots, we can use wspace=1 in subplots_adjust() method without tight plot layout.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots with 4 indices.To adjust the vertical space, we can use wspace=1.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(0, 2 * np.pi, 400) y = np.sin(x ** 2) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2) fig.subplots_adjust(wspace=1) ... Read More

Get List of Font Family or Names in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 10:01:51

882 Views

o get the list of font family in Matplotlib, we can take the following steps −Iterate fonts manager ttflist and print the names.Iterate fonts manager afmlist and print the names.Exampleimport matplotlib.font_manager as fm for f in fm.fontManager.ttflist:    print(f.name) for f in fm.fontManager.afmlist:    print(f.name)OutputSTIXNonUnicode STIXGeneral STIXSizeFiveSym cmr10 ... ... ... Nimbus Sans L Bitstream Charter Nimbus Sans L Nimbus Sans L

Pass Parameters to on_key in Fig Canvas MPL Connect Key Press Event

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:53:10

665 Views

To pass parameters to on_key in fig.canvas.mpl_connect('key_press_event', on_key), 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.Set x and y scale of the axes.Bind the function to the event.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.set_xlim(0, 10) ax.set_ylim(0, 10) def onkey(event):    if event.key.isalpha():       if event.xdata is not None and event.ydata is not None:          ax.plot(event.xdata, event.ydata, 'bo-')       ... Read More

Customizing Annotation with Seaborn's FacetGrid

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:51:20

484 Views

To customizing annotation with seaborn's face grid, we can take following steps −Set the figure size and adjust the padding between and around the subplots.Create a data frame with col1 and col2 columns.Multi-plot grid for plotting conditional relationships.Apply a plotting function to each facet's subset of the data.Set the title of each grids.To display the figure, use show() method.Exampleimport pandas as pd import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'col1': [3, 7, 8, 1], 'col2': ["three", "seven", "one", "zero"]}) g = sns.FacetGrid(data=df, col='col2', height=3.5) g.map(plt.hist, 'col1', ... Read More

Advertisements