Found 1034 Articles for Matplotlib

Matplotlib legends in subplot

Rishikesh Kumar Rishi
Updated on 26-Oct-2023 03:33:10

25K+ Views

To add legends in a subplot, we can take the following Steps −Using numpy, create points for x, y1, y2 and y3.Create a figure and a set of subplots, using the subplots() method, considering 3 subplots.Plot the curve on all the subplots(3), with different labels, colors. To place the legend for each curve or subplot adding label.To activate label for each curve, use the legend() method.To display the figure, use the 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(-2, 2, 100) y1 = np.sin(x) y2 = np.cos(x) y3 ... Read More

How to update matplotlib's imshow() window interactively?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:50:08

4K+ Views

To plot interactive matplotlib’s imshow window, we can take the following steps −Using the subplots() method, create a figure and a set of subplots.Create an array to plot an image, using numpy.Display the image using the imshow() method.To make a slider axis, create an axes and a slider, with facecolor=yellow.To update the image, while changing the slider, we can write a user-defined method, i.e., update(). Using the draw_idle() method, request a widget redraw once the control returns to the GUI event loop.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt from matplotlib.widgets import Slider ... Read More

Plot curves to differentiate antialiasing in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:47:44

183 Views

To differentiate antialiasing through curves, we can take the following Steps −Add a subplot to the current figure, using the subplot() method, where nrows=1, ncols=2 and index=1.Plot the curve using the plot() method, where antialiased flag is false and color is red.Place the legend at the upper-left corner using the legend() method.Add a subplot to the current figure, using the subplot() method, where nrows=1, ncols=2 and index=2.Plot the curve using the plot() method, where antialiased flag is true and color is green.Place the legend at the upper-right corner using the legend() method.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt ... Read More

Get the legend as a separate picture in Matplotlib

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:43:34

3K+ Views

To get the legend as a separate picture, we can take the following steps −Create x and y points using numpy.Using the figure() method, create a new figure, or activate an existing figure for Line plot and Legend plot figures.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, using the add_subplot() method at nrow=1, ncols=1 and at index=1.Create line1 and line2 using x, y and y1 points.Place the legend for line1 and line2, set ordered labels, put at center location.Save the figure only with legend using the savefig() method.Exampleimport numpy as np from matplotlib import pyplot as plt x = np.linspace(1, 100, ... Read More

Logarithmic Y-axis bins in Python

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:38:49

3K+ Views

To plot logarithmic Y-axis bins in Python, we can take the following steps −Create x and y points using numpy.Set the Y-axis scale using the yscale() method.Plot the x and y points, using the plot() method with linestyle="dashdot" and label="y=log(x)".To activate the label of the line, use the legend() method.To display the figure, use the 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(1, 100, 1000) y = np.log(x) plt.yscale('log') plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=log(x)") plt.legend() plt.show()OutputRead More

How to hide axes and gridlines in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-Sep-2023 01:31:20

37K+ Views

To hide axes (X and Y) and gridlines, we can take the following steps −Create x and y points using numpy.Plot a horizontal line (y=0) for X-Axis, using the plot() method with linestyle, labels.Plot x and y points using the plot() method with linestyle, labels.To hide the grid, use plt.grid(False).To hide the axes, use plt.axis('off')To activate the labels' legend, use the legend() method.To display the figure, use the 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(-10, 10, 50) y = np.sin(x) plt.axhline(y=0, c="green", linestyle="dashdot", label="y=0") plt.plot(x, y, c="red", lw=5, linestyle="dashdot", label="y=sin(x)") plt.grid(False) plt.axis('off') ... Read More

How to plot matplotlib contour?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:33:48

276 Views

To plot matplotlib contour, we can take the following steps −Create data points for x, y and h using numpy.Using the countourf() method, create a colored 3D (alike) plot.Using the set_over() method, set the color for high out-of-range values when "norm.clip = False".Using the set_under() method, set the color for low out-of-range values when "norm.clip = False".Using the changed() method, call this whenever the mappable is changed to notify all the callbackSM listeners to the "changed" signal.Use the show() method to display the figure.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = ... Read More

How to display a 3D plot of a 3D array isosurface in matplotlib mplot3D or similar?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:31:20

821 Views

Let's take an example to see how to display a 3D plot of a 3D array isosurface in matplotlib −Exampleimport numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(-5, 5, 0.25) y = np.arange(-5, 5, 0.25) x, y = np.meshgrid(x, y) h = x ** 2 + y ** 2 fig = plt.figure() ax = Axes3D(fig) ax.plot_surface(x, y, h, rstride=1, cstride=1, cmap=plt.cm.rainbow, linewidth=0, antialiased=False) plt.show()Output

How to save the plot to a numpy array in RGB format?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:29:13

939 Views

To save the plot to a numpy array in RGB format, we can take the following steps −Create r, g and b random array using numpy.Zip r, g and b (grom step 1) to make an rgb tuple list.Convert rgb into a numpy array to plot it.Plot the numpy array that is in rgb format.Save the figure at the current location.To display the figure, use the 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 r = np.random.rand(100) g = np.random.rand(100) b = np.random.rand(100) rgb = zip(r, g, b) arr = np.array([item for item in rgb]) plt.plot(arr) plt.savefig("myplot.png") ... Read More

How to set the range of Y-axis for a Seaborn boxplot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 13:12:01

9K+ Views

To set the range of Y-axis for a Seaborn boxplot, we can take the following steps −Using set_style() method, set the aesthetic style of the plots.Load the dataset using load_dataset("tips"); need Internet.Using boxplot(), draw a box plot to show distributions with respect to categories.To set the range of Y-axis, use the ylim() method.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.boxplot(x="day", y="total_bill", data=tips) plt.ylim(5, 50) plt.show()OutputRead More

Advertisements