Programming Articles - Page 1197 of 3363

How to display Matplotlib Y-axis range using absolute values rather than offset values?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:44:24

1K+ Views

To display Y-axis range using absolute values rather than offset values, we can take the following steps −Create x_data and y_data data points in the range of 100 to 1000.Create a figure and a set of subplots using subplots() method.Plot x_data and y_data using plot() method.If a parameter is not set, the corresponding property of the formatter is left unchanged using ticklabel_format() method with useOffset=False.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_date = range(100, 1000, 100) y_data = range(100, 1000, 100) fig, ax = plt.subplots() ax.plot(x_date, y_data) ax.ticklabel_format(useOffset=False) plt.show()OutputRead More

Drawing lines between two plots in Matplotlib

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

2K+ Views

To draw lines between two plots in matplotlib, we can take the following steps −Create a new figure or activate an existing figure.Add two axes (ax1 and ax2) to the figure as part of a subplot arrangement.Create random data x and y using numpy.Plot x and y data points on both the axes (ax1 and ax2) with color=red and marker=diamond.Initialize two variables, i and j to get the diffirent data points on the subplot.Make xy and mn tuple for positions to add a patch on the subplots.Add a patch that connects two points (possibly in different axes), con1 and con2.Add artists for con1 ... Read More

How to display only a left and bottom box border in Matplotlib?

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

4K+ Views

To display or hide box border in matplotlib, we can use spines (value could be right, left, top or bottom) and set_visible() method to set the visibility to True or False.StepsCreate x and y data points using numpy.Create a figure and add a set of subplots using subplots() method.Plot x and y data points using plot() method, where linewidth=7 and color=red.Set visibility as True for left and bottom, and False for top and right.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(-2, 2, 10) y ... Read More

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

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

3K+ 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

8K+ 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

356 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

1K+ 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

39K+ 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

394 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

620 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. ]

Advertisements