Found 1034 Articles for Matplotlib

How do I adjust (offset) the colorbar title in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:30:32

2K+ Views

To adjust (offset) the colorbar title in matplotlib, we can take the following steps −Create a random data of 4×4 dimension.Use imshow() method to display the data as an imgage.Create a colorbar for a scalar mappable instance using colorbar() method, with im mappable instance.Now, adjust (offset) the colorbar title in matplotlib, with labelpad=-1. You can assign different values to labelpad to see how it affects the colorbar title.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, cm plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) im = plt.imshow(data, cmap=cm.jet) cb = plt.colorbar(im) cb.set_label('Image Colorbar', labelpad=-1) plt.show()OutputRead More

How to increase plt.title font size in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:28:01

7K+ Views

To increase plt.title font size, we can initialize a variable fontsize and can use it in the title() method's argument.StepsCreate x and y data points using numpy.Use subtitle() method to place the title at the center.Plot the data points, x and y.Set the title with a specified fontsize.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 10) y = x ** 2 fontsize = 12 plt.suptitle("Quadratic Equation", fontsize=fontsize) plt.plot(x, y) plt.title("y=x$^{2}$", fontdict={'fontsize': fontsize}) plt.show()OutputRead More

How do I configure the behavior of the Qt4Agg backend in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:27:41

268 Views

To configure the behaviour of the backend, we can use matplotlib.rcParams['backend'] with a new backend name.StepsUse get_backend() method to get the backend name.Override the existing backend name using matplotlib.rcParams.Use get_backend() method to get the configured backend name.Exampleimport matplotlib backend = matplotlib.get_backend() print("The current backend name is: ", backend) matplotlib.rcParams['backend'] = 'TkAgg' backend = matplotlib.get_backend() print("Configured backend name is: ", backend)OutputThe current backend name is: GTK3Agg Configured backend name is: TkAgg

How to change the strength of antialiasing in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:27:21

715 Views

We can change the strength of antialiasing by using True or False flag in the argument of plot() method.StepsCreate x data points and colors list with different colors.Defining a method that accepts antialiased flag and axis.We can iterate in the range of 5, to print 5 different colors of curves from x data points (Step 1).Create a new figure or activate an existing figure.Add an axis to the figure as part of a subplot arrangement, at index 1.Plot a line with antialiased flag set as False and ax1 (axis 1) and set the title of the figure.Add an axis to the figure ... Read More

How to clear the memory completely of all Matplotlib plots?

Rishikesh Kumar Rishi
Updated on 14-Sep-2023 15:47:36

33K+ Views

Using the following methods, we can clear the memory occupied by Matplotlib plots.plt.figure() - Create a new figure or activate an existing figure.plt.figure().close() -  Close a figure window.close() by itself closes the current figureclose(h), where h is a Figure instance, closes that figureclose(num) closes the figure number, numclose(name), where name is a string, closes figure with that labelclose('all') closes all the figure windowsplt.figure().clear() - It is the same as clf.plt.cla() - Clear the current axes.plt.clf() - Clear the current figure.Examplefrom matplotlib import pyplot as plt fig = plt.figure() plt.figure().clear() plt.close() plt.cla() plt.clf()OutputWhen we execute the code, it will clear all the plots from ... Read More

What is the preferred way to set Matplotlib figure/axes properties?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:51:09

279 Views

To set the properties of a plot, we can get the current axis of the plot. After that, we can perform several set_* methods to set the properties of the plot.StepsCreate a figure and a set of subplots using subplots() method with figsize=(5, 5).Create x and y data points using numpy.Plot x and y using plot() method.Set the title and labels (for X and Y axis) using set_xlabel() and set_ylabel() methods.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-1, 1, 10) y = ... Read More

How do I apply some function to a Python meshgrid?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:25:51

468 Views

Meshgrid − Coordinate matrices from coordinate vectors.Let's take an example to see how we can apply a function to a Python meshgrid. We can consider two lists, x and y, using numpy vectorized decorator.Exampleimport numpy as np @np.vectorize def foo(a, b):    return a + b x = [0.0, 0.5, 1.0] y = [0.0, 1.0, 8.0] print("Function Output: ", foo(x, y))OutputFunction Output: [0. 1.5 9. ]

What is the necessity of plt.figure() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:25:30

238 Views

Using plt.figure(), we can create multiple figures and to close them all explicitly, call plt.close(). If you are creating many figures, make sure you explicitly call pyplot.close on the figures you are not using, because this will enable pyplot to properly clean up the memory.Using subplots(), we can create a figure and set of subplots.Here we creating two figures, fig1 and fig2. fig1 is 8×8 in size, whereas fig2 has the default figsize. There are 4×4=16 subplots added in fig2.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig2, ax_lst = plt.subplots(4, 4) plt.show()OutputRead More

How can I plot a single point in Matplotlib Python?

Rishikesh Kumar Rishi
Updated on 23-Aug-2023 14:04:08

62K+ Views

To plot a single data point in matplotlib, we can take the following steps −Initialize a list for x and y with a single value.Limit X and Y axis range for 0 to 5.Lay out a grid in the current line style.Plot x and y using plot() method with marker="o", markeredgecolor="red", markerfacecolor="green".To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [4] y = [3] plt.xlim(0, 5) plt.ylim(0, 5) plt.grid() plt.plot(x, y, marker="o", markersize=20, markeredgecolor="red", markerfacecolor="green") plt.show()OutputRead More

How to adjust transparency (alpha) in Seaborn pairplot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:15:49

5K+ Views

To adjust transparency, i.e., aplha in Seaborn pairplot, we can change the value of alpha.StepsCreate a dataframe using Pandas with two keys, col1 and col2.Initialize the variable, alpha, for transparency.Use pairplot() method to plot pairwise relationships in a dataset. Use df (from step 1), kind="scatter", and set the plot size, edgecolor, facecolor, linewidth and alpha vaues in the arguments.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.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({"col1": [1, 3, 5, 7, 1], "col2": [1, 5, 7, 9, 1]}) alpha = 0.75 ... Read More

Advertisements