- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1034 Articles for Matplotlib
3K+ Views
To draw axis in the middle of a figure, we can take the following steps −Create x and sqr data points using numpy.Create a new figure, or activate an existing figure, using figure() method.Add an axis to the figure as a part of a subplot arrangement.Set the postion of left and bottom spines.Set the color of the right and top spines.Plot x and sqr, using plot() method, with label y=x2 and color=red.Place the legend using legend() method. Set the location at upper right corner.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 ... Read More
3K+ Views
To create a graph with date and time in axis labels, we can take the following steps−Create a figure and add a set of subplots.Create x and y data points using numpy.Set date formatter for X-axis.Plot x and y using plot() method.Set the ticks of X-axis.Set the date-time tick labels for X-axis, with some rotation.Make the plot tight layout using plt.tight_layout() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, dates import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i ... Read More
3K+ Views
To plot 1D data at a given Y-value with pyplot, we can take the following steps−Initialize y value.Create x and y data points using numpy. zeros_like helps to return an array of zeros with the same shape and type as a given array and add y-value for y data points.Plot x and y with linestyle=dotted, color=red, and linewidth=5.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 y_value = 1 x = np.arange(10) y = np.zeros_like(x) + y_value plt.plot(x, y, ls='dotted', c='red', lw=5) plt.show()OutputRead More
11K+ Views
To specify values on Y-axis in Python, we can take the following steps−Create x and y data points using numpy.To specify the value of axes, create a list of characters.Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively.Plot the line using x and y, color=red, using plot() method.Make x and y margin 0.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.array([0, 2, 4, 6]) y = np.array([1, 3, 5, 7]) ticks = ... Read More
9K+ Views
To insert a degree symbol into a plot, we can use LaTeX representation.StepsCreate data points for pV, nR and T using numpy.Plot pV and T using plot() method.Set xlabel for pV using xlabel() method.Set the label for temperature with degree symbol using ylabel() method.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 pV = np.array([3, 5, 1, 7, 10, 9, 4, 2]) nR = np.array([31, 15, 11, 51, 12, 71, 41, 13]) T = np.divide(pV, nR) plt.plot(pV, T, c="red") plt.xlabel("Pressure x Volume") plt.ylabel("Temperature ($^\circ$C)") plt.show()OutputRead More
2K+ Views
To use extent in matplotlib imshow(), we can use extent [left, right, bottom, top].StepsCreate random data using numpy.Display the data as an image, i.e., on a 2D regular raster with data and extent [−1, 1, −1, 1] arguments.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 data = np.random.rand(4, 4) plt.imshow(data, extent=[-1, 1, -1, 1]) plt.show()Output
2K+ Views
To save a file with legend outside the plot, we can take the following steps −Create x data points using numpy.Plot y=sin(x) curve using plot() method, with color=red, marker="v" and label y=sin(x).Plot y=cos(x), curve using plot() method, with color=green, marker="x" and label y=cos(x).To place the legend outside the plot, use bbox_to_anchor(.45, 1.15) and location="upper center".To save the figure, use savefig() 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, 100) plt.plot(x, np.sin(x), c="red", marker="v", label="y=sin(x)") plt.plot(x, np.cos(x), c="green", marker="x", label="y=cos(x)") plt.legend(bbox_to_anchor=(.45, 1.15), loc="upper center") plt.savefig("legend_outside.png")OutputWhen we execute this code, it will ... Read More
2K+ Views
To change scale of a table, we can use the scale() method. Steps −Create a figure and a set of subplots, nrows=1 and ncols=1.Create a random data using numpy.Make columns value.Make the axis tight and off.Initialize a variable fontsize to change the fontsize.To set the fontsize of the table and to scale the table, we can use 1.5 and 1.5.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 fig, axs = plt.subplots(1, 1) data = np.random.random((10, 3)) columns = ("Column I", "Column II", "Column III") axs.axis('tight') ... Read More
3K+ Views
To reduce the chances of overlapping between x and y tick labels in matplotlib, we can take the following steps −Create x and y data points using numpy.Add a subplot to the current figure at index 1 (nrows=1 and ncols=2).Set x and y margins to 0.Plot x and y data points and add a title to this subplot, i.e., "Overlapping".Add a subplot to the current figure at index 2 (nrows=1 and ncols=2).Set x and y margins to 0.Plot x and y data points and add a title to this subplot, i.e., "Non Overlapping".The objective of MaxNLocator and prune ="lower" is that the smallest tick will be removed.To display the figure, ... Read More
181 Views
To style a part of label in legend, we can take the following steps −Create data point for x using numpy.Plot a sine curve using np.sin(x) with a text label.Plot a cosine curve using np.cos(x) with a text label.To place the legend on the plot, use legend() method.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) plt.plot(x, np.sin(x), label="This is $\it{a\ sine\ curve}$") plt.plot(x, np.cos(x), label="This is $\bf{a\ cosine\ curve}$") plt.legend(loc='lower right') plt.show()OutputRead More