Found 1034 Articles for Matplotlib

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

How to 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

How to make two markers share the same label in the legend using Matplotlib?

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

986 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

838 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

How to get the list of font family (or Name of Fonts) in Matplotlib?

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

653 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

How can I pass parameters to on_key in fig.canvas.mpl_connect('key_press_event',on_key)?

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

496 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

355 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

Manipulation on vertical space in Matplotlib subplots

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:48:05

3K+ Views

To manipulate on vertical space in Matplotlib subplots, we can use hspace=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 hspace=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(hspace=1) ... Read More

How to convert data values into color information for Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:46:24

840 Views

To convert data values into color information for Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Get a colormap instance, defaulting to rc values if *name* is None.Create random values that could be converted into color information.Create random data points, x and y.Use scatter() method to plot x and y.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 plasma = plt.get_cmap('GnBu_r') values = np.random.rand(100) x = np.random.rand(len(values)) y = np.random.rand(len(values)) sc = plt.scatter(x, ... Read More

Interactive plotting with Python Matplotlib via command line

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:44:34

1K+ Views

To get interactive plots, we need to activate the figure. Using plt.ioff() and plt.ion(), we can perform interactive actions with a plot.Open Ipython shell and enter the following commands on the shell.ExampleIn [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as In [3]: fig, ax = plt.subplots() # Diagram will pop up. Let’s interact. In [4]: ln, = ax.plot(range(5)) # Drawing a line In [5]: ln.set_color("orange") # Changing drawn line to orange In [6]: plt.ioff() # Stopped interaction In [7]: ln.set_color("red") # Since we have stopped the interaction ... Read More

Advertisements