Found 1034 Articles for Matplotlib

How to name different lines in the same plot of Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:25:14

1K+ Views

To name different lines in the same plot of matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make two lists of data points.Plot point1 and point2 using plot() method.Place a legend on the figure.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 points1 = [2, 4, 1, 5, 1] points2 = [3, 2, 0, 4, 3] plt.plot(points1, 'g--', label="plot A") plt.plot(points2, 'r-o', label="plot A") plt.legend() plt.show()Output

Drawing circles on an image with Matplotlib and NumPy

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:24:09

2K+ Views

To draw a circle on an image with matplotlib and numpy, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Read an image from a file into an array.Create x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Display data as an image, i.e., on a 2D regular raster using imshow() method.Turn off the axes.Add patches on the current axes.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Circle plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

How to change the legend fontname in Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:22:28

561 Views

To change the legend fontname 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.Plot x, sin(x) and cos(x) using plot() method.Use legend() method to place the legend.Iterate legend.get_texts() and update the legend fontname.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 x = np.linspace(-5, 5, 100) plt.plot(x, np.sin(x), label="$y=sin(x)$") plt.plot(x, np.cos(x), label="$y=cos(x)$") legend = plt.legend(loc='upper right') i = 1 for t in legend.get_texts():   ... Read More

Adding units to heatmap annotation in Seaborn

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:21:11

490 Views

To add units to a heatmap annotation in Seaborn, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a 5×5 dimension matrix using numpy.Plot rectangular data as a color-encoded matrix.Annotate heatmap value with %age unit.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import seaborn as sns import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(5, 5) ax = sns.heatmap(data, annot=True, fmt='.1f', square=1, linewidth=1.) for t in ax.texts: t.set_text(t.get_text() + " %") plt.show()OutputRead More

How to color a Matplotlib scatterplot using a continuous value?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:19:56

6K+ Views

To color a matplotlib scatterplot using continuous value, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z random data points using numpy.Create a figure and a set of subplots.Create a scatter plot.Draw a colorbar in an existing axes, with scatter points scalar mappable instance.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, y, z = np.random.rand(3, 50) f, ax = plt.subplots() points = ax.scatter(x, y, c=z, s=50, cmap="plasma") f.colorbar(points) ... Read More

Text alignment in a Matplotlib legend

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:18:42

3K+ Views

To make text alignment in a matplotlib legend, 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.Plot x, sin(x) and cos(x) using plot() method.Place legend using legend() method and initialize a method.Iterate the legend.get_texts() method to set the horizontal alignment.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 x = np.linspace(-5, 5, 100) plt.plot(x, np.sin(x), label="$y=sin(x)$") plt.plot(x, np.cos(x), label="$y=cos(x)$") legend = plt.legend(loc='upper right') for t in ... Read More

Plot Matplotlib 3D plot_surface with contour plot projection

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:16:49

597 Views

To plot 3d plot_surface with contour plot projection, we can use plot_surface() and contourf() methods.StepsSet the figure size and adjust the padding between and around the subplots.Create x, y, X, Y and Z data points using numpy.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, with 3D projection.Use plot_surface() method to create a surface plot.Create a 3D filled contour plotm using contourf() method.Trurn off the axes.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] ... Read More

How to add Matplotlib Colorbar Ticks?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:15:23

5K+ Views

To add ticks to the colorbar, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z data points using numpy.Use imshow() method to display the data as an image, i.e., on a 2D regular raster.Create ticks using numpy in the range of min and max of z.Create a colorbar for a ScalarMappable instance, *mappable*, with ticks=ticks.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, y = np.mgrid[-1:1:100j, -1:1:100j] z = (x + ... Read More

How to change the font properties of a Matplotlib colorbar label?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:14:11

3K+ Views

To change the font properties of a matplotlib colorbar label, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z 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, *mappable*.Using colorbar axes, set the font properties such that the label is bold.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, y = np.mgrid[-1:1:100j, -1:1:100j] z = ... Read More

How can I draw a scatter trend line using Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:12:46

5K+ Views

To draw a scatter trend line using matplotlib, we can use polyfit() and poly1d() methods to get the trend line points.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.Plot x and y data points using numpy.Find the trend line data points using polyfit() and poly1d() method.Plot x and p(x) data points using plot() 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 x = np.random.rand(100) y ... Read More

Advertisements