- 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
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
573 Views
To plot a rectangle inside a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure method.Add a subplot to the current axis.Make a rectangle and a circle instance using Rectangle() and Circle() class.Add a patch on the axes.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rect1 = patches.Rectangle((-2, -2), 4, 2, color='yellow') circle1 = matplotlib.patches.Circle((0, 0), radius=3, color='red') ax.add_patch(circle1) ax.add_patch(rect1) plt.xlim([-5, 5]) plt.ylim([-5, 5]) plt.axis('equal') plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
339 Views
set_xlim − Set the X-axis view limits.set_xbound − Set the lower and upper numerical bounds of the X-axis.To set the xlim and xbound, we can take the following steps −Using subplots(2), we can create a figure and a set of subplots. Here, we are creating 2 subplots.Create x and y data points using numpy.Use axis 1 to plot x and y data points using plot() method.Set x limit using set_xlim() method.Use axis 2 to plot x and y data points using plot() method.Sex xbound using set_xbound() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To animate pcolormesh in matplotlib, we can take the following steps −Create a figure and a set of subplots.Create x, y and t data points using numpy.Create X3, Y3 and T3, return coordinate matrices from coordinate vectors using meshgrid.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.Make a colorbar with colormesh axis.Animate pcolormesh using Animation() class method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-3, 3, 91) t = np.linspace(0, 25, 30) y = np.linspace(-3, 3, 91) X3, Y3, T3 = ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To add a colorbar for hist2d plot, we can pass a scalar mappable object to colorbar() method's argument.StepsCreate x and y data points using numpy.Create a figure and a set of subplots using subplots() method.Make a 2D histogram plot using hist2d() method.Create a colorbar for a hist2d scalar mappable instance.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, colors plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(100) y = np.random.randn(100) + 5 fig, ax = plt.subplots() hh = ax.hist2d(x, y, bins=40, norm=colors.LogNorm()) fig.colorbar(hh[3], ax=ax) plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To plot a density map in Python, we can take the following steps −Create side, x, y, and z using numpy. Numpy linspace helps to create data between two points based on a third number.Return coordinate matrices from coordinate vectors using side data.Create exponential data using x and y (Step 2).Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, cm, colors import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True side = np.linspace(-2, 2, 15) X, Y = np.meshgrid(side, side) Z = np.exp(-((X - 1) ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
30K+ Views
To write text in subscript in the axis labels and the legend, we can take the following steps −Create x and y data points using NumPy.Plot x and y data points with a super subscript texts label.Use xlabel and ylabel with subscripts in the text.Use the legend() method to place a legend in the plot.Adjust the padding between and around subplots.To display the figure, use the show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 1000) y = np.exp(x) plt.plot(x, y, label=r'$e^x$', c="red", lw=2) plt.xlabel("$X_{axis}$") plt.ylabel("$Y_{axis}$") plt.legend(loc='upper left') plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To set axis ticks in multiples of pi in Python, we take following steps −Initialize a pi variable, create theta and y data points using numpy.Plot theta and y using plot() method.Get or set the current tick locations and labels of the X-axis using xticks() method.Convenience method to set or retrieve autoscaling margins using margins() 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 pi = np.pi theta = np.arange(-2 * pi, 2 * pi+pi/2, step=(pi / 2)) y = np.sin(theta) plt.plot(theta, y) plt.xticks(theta, ['-2π', '-3π/2', 'π', ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
4K+ Views
To make hollow square marks with matplotlib, we can use marker 'ks', markerfacecolor='none', markersize=15, and markeredgecolor=red.StepsCreat x and y data points using numpy.Create a figure or activate an existing figure, add an axes to the figure as part of a subplot arrangement.Plot x and y data points using plot() method. To make hollow square marks, we can use marker "ks" and markerfacecolor="none", markersize="15" and markeredge color="red".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 = np.sin(x) fig = plt.figure() ax1 = ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
6K+ Views
To display all label values, we can use set_xticklabels() and set_yticklabels() methods.StepsCreate a list of numbers (x) that can be used to tick the axes.Get the axis using subplot() that helps to add a subplot to the current figure.Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).Set tick labels with label lists (["one", "two", "three", "four"]) and rotation of 45 using set_xticklabels() and set_yticklabels().To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to add space. Argument direction (in) helps to put ticks inside ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
712 Views
StepsUsing the subplots() method, create a figure and a set of subplots with figure size (7, 7).Create a data frame with two keys, time and speed.Get the size of the array.Add a table to the current axis using the table method.Shrink the font size until the text fits into the cell width.Set the font size in the table.Set the face color, edge color, and text color by iterating the matplotlib table.Save and display the figure.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), ... Read More