Found 1034 Articles for Matplotlib

How to set a title above each marker which represents a same label in Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 10:29:44

246 Views

To set a title above each marker which represents the same label in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x data points using Numpy.Create four curves, c1, c2, c3 and c4 using plot() method.Place a legend on the figure, such that the same label marker would come together.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, legend_handler plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-10, 10, 100) c1, = plt.plot(x, np.sin(x), ls='dashed', label='y=sin(x)') c2, ... Read More

How to give Matplolib imshow plot colorbars a label?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 10:31:06

4K+ Views

To give matplotlib imshow() plot colorbars a label, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create 5×5 data points using Numpy.Use imshow() method to display the data as an image, i.e., on a 2D regular raster.Create a colorbar for a ScalarMappable instance, im.Set colorbar label using set_label() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(5, 5) im = plt.imshow(data, cmap="copper") cbar = plt.colorbar(im) cbar.set_label("Colorbar") plt.show()OutputRead More

How to decrease the hatch density in Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 10:33:17

542 Views

To decrease the hatch density in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a customized horizontal hatch class to override the density.Append the horizontal hatch class.Create a new figure or activate an existing figure.Add an 'ax1' to the figure as part of a subplot arrangement.Make lists of data points.Make a bar plot with x and ydata points, with hatch='o', color='green' and edgecolor='red'.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, hatch plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True class MyHorizontalHatch(hatch.HorizontalHatch):    def ... Read More

How to make a quiver plot in polar coordinates using Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 10:55:51

2K+ Views

To make a quiver plot in polar coordinates using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create radii, thetas, theta and r data points using numpy.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Make poly collections of arrows.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True radii = np.linspace(0, 1, 5) thetas = np.linspace(0, 2 * np.pi, 20) theta, r = ... Read More

How to have a function return a figure in Python (using Matplotlib)?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 11:07:08

14K+ Views

To have a function return a figure in Python (using 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 using numpy.Make a function plot(x, y) that creates a new figure or activate an existing figure using figure() method.Plot the x and y data points using plot() method; return fig instance.Call plot(x, y) method and store the figure instance in a variable, f.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

What is the correct way to replace matplotlib tick labels with computed values?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 11:08:28

268 Views

We can use ax.loglog(x, y) and set_major_formatter() methods to replace tick labels with computed values.StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make a plot with log scaling on both the X and Y axis.Set the formatter of the major ticker.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, ticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.loglog(np.logspace(0, 5), np.logspace(0, 5)**2) ax.xaxis.set_major_formatter(ticker.LogFormatterExponent()) plt.show()OutputRead More

How to make a simple lollipop plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 11:10:59

375 Views

To make a simple lollipop plot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Make an ordered dataframe, using sort_values().Make a list in the range of dataframe index.Create a stem plot, using the ordered dataframe.Set xticks and labels using xticks() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'group': list(map(chr, range(65, 85))), 'values': np.random.uniform(size=20)}) ... Read More

How to put the title at the bottom of a figure in Matplotlib?

Rishikesh Kumar Rishi
Updated on 23-Sep-2021 11:12:10

6K+ Views

To put the line title at the bottom of a figure in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable, N, to get the number of sample data.Plot the x and y data points using scatter() method.Set the title at the bottom of the figure in matplotlib, with y=-0.01.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True N = 100 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y, c=x, ... Read More

How to make multipartite graphs using networkx and Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Aug-2021 07:20:47

1K+ Views

To make multipartite graph in networkx, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a list of subset sizes and colors.Define a method for multilayered graph that could return a multilayered graph object.Set the color of the nodes.Position the nodes in layers of straight lines.Draw the graph G with Matplotlib.Set equal axis properties.To display the figure, use show() method.Exampleimport itertools import matplotlib.pyplot as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True subset_sizes = [5, 5, 4, 3, 2, 4, 4, 3] subset_color = ... Read More

How to plot the difference of two distributions in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Aug-2021 07:19:23

2K+ Views

To plot the difference of two distributions in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a and b datasets using Numpy.Get kdea and kdeb, i.e., representation of a kernel-density estimate using Gaussian kernels.Create a grid using Numpy.Plot the gird with kdea(grid), kdeb(grid) and kdea(grid)-kdeb(grid), using plot() method.Place the legend at the upper-left corner.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import scipy.stats plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True a = np.random.gumbel(50, 28, 100) b = np.random.gumbel(60, 37, 100) ... Read More

Advertisements