- 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
4K+ Views
To plot black-and-white binary map in matplotlib, we can create and add two subplots to the current figure using subplot() method, where nrows=1 and ncols=2. To display the data as a binary map, we can use greys colormap in imshow() method.StepsCreate data using numpyAdd two sublots, nrows=1 and ncols=2. Consider index 1.To show colored image, use imshow() method.Add title to the colored map.Add a second subplot at index 2.To show the binary map, use show() method with Greys colormap.To adjust the padding between and around the subplots, use tight_layout() method.To display the figure, use show() method.Exampleimport numpy as np from ... Read More
2K+ Views
To create a legend for a contour plot in matplotlib, we can take the following steps−Create x, y and z data points to plot the contour function.To create a 3D filled contour plot, we can use contourf() method with x, y, z and different levels.Make a list of rectangle with the returned contour signature collection and set face colorNow, place the legend in the plot using proxy (of step 3) and different labels.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, y = np.meshgrid(np.arange(10), np.arange(10)) ... Read More
5K+ Views
To change the text color of font in the legend in matplotlib, we can take the following steps−Create x and y data points using numpy.Plot x and y using plot() method, where color of line is red and label is "y=exp(x)".To place the legend, use legend() method with location of the legend and store the returned value to set the color of the text.To set the color of the text, use set_color() method with green color.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 = ... Read More
651 Views
To plot scatter points with increasing size of marker, we can take the following steps−StepsCreate x and y data pointsTo get increasing size of marker, make a list of numbers.Use scatter method to plot scatter points.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 = [0, 2, 4, 6, 8, 10] y = [0] * len(x) s = [10 * 4 ** n for n in range(len(x))] plt.scatter(x, y, s=s, c='red') plt.show()Output
6K+ Views
To plot only a table, we can take the following steps−Create fig and axs, using subplots. Create a figure and a set of subplots.Create random data for 10 rows and 3 columns.Create a tuple for columns name.axis('tight') − Set the limits, just large enough to show all the data, then disable further autoscaling.axis('off') − Turn off axis lines and labels. Same as ''False''.To add a table on the axis, use table() instance, with column text, column labels, columns, and location=center.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"] ... Read More
3K+ Views
To add vertical lines to a distribution plot, we can take the following steps−Create a list of numbers.Create an axis using sns.displot().Get x and y data of the axis ax.Plot a vertical line on the plot.Remove the line at the 0th index.To display the figure, use show() method.Exampleimport seaborn as sns, numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [5, 6, 7, 2, 3, 4, 1, 8, 2] ax = sns.distplot(x, kde=True) x = ax.lines[0].get_xdata() y = ax.lines[0].get_ydata() plt.axvline(x[np.argmax(y)], color='red') ax.lines[0].remove() plt.show()OutputRead More
5K+ Views
To get a list of axes of a figure, we will first create a figure and then, use get_axes() method to get the axes and set the labels of those axes.Create xs and ys using numpy and fig using figure() method. Create a new figure, or activate an existing figure.Use add_subplot() method. Add an '~.axes.Axes' to the figure as part of a subplot arrangement, where nrows=1, ncols=1 and index=1.. Get the axes of the fig, and set the xlabel and ylabel.Plot x and y data points with red color.To display the figure, use show() method.Exampleimport numpy as np from matplotlib ... Read More
7K+ Views
To draw a log-normalized imshow() plot with a colorbar representing the raw data in matplotlib, we can take the following steps −Create a 2D array using numpy.Display the data as an image, i.e., on a 2D regular raster, using imshow() methodCreate a colorbar for a ScalarMappable instance, *mappable*, using imshow() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, cm from matplotlib import colors 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.rainbow, norm=colors.LogNorm()) plt.colorbar(im) plt.show()OutputRead More
2K+ Views
To plot circular (polar) histogram in Python, we can take the following steps−Create data points for theta, radii and width using numpy.Add a subplot to the current figure, where projection='polar' and nrows=1, ncols=1 and index=1.. Make a bar plot using bar() method, with theta, radii and width data pointsIterate radii and bars after zipping them together and set the face color of the bar and the alpha value. Lesser the alpha value, greater the transparency.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 N = 20 theta = ... Read More
1K+ Views
To add different graphs (as an inset) in another Python graph, we can take the following steps −Create x and y data points using numpy.Using subplots() method, create a figure and a set of subplots, i.e., fig and ax.To create a new axis, add axis to the existing figure (Step 2).Plot x and y on the axis (Step 2).Plot x and y on the new axis (Step 3).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, 100) y = np.sin(x) fig, ax = plt.subplots() left, bottom, width, height = [.30, 0.6, ... Read More