- 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)
360 Views
To change order of plots in Pandas hist commad, we can take the following steps −Make a data frame using Pandas.Plot a histogram with the data frame.Plot the data frame in different order.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'a': [1, 1, 1, 1, 3], 'b': [1, 1, 2, 1, 3], 'c': [2, 2, 2, 1, 3], }) df.hist() df[['c']].hist() df[['a']].hist() df[['b']].hist() plt.show()Output
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To put text outside a plot, we can change the text position by changing the value of text_pos_x and text_pos_yStepsCreate data points for x and y.Initialize the text position of x and y.To plot x and y, use plot() method with color='red'.Use text() method to add text to figure.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, 5, 100) y = np.exp(x) text_pos_x = 0.60 text_pos_y = 0.50 plt.plot(x, y, c='red') plt.text(text_pos_x, text_pos_y, "$\mathit{y}=e^{x}$", fontsize=14, transform=plt.gcf().transFigure, color='green') plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
853 Views
To make grouped labels for bar charts, we can take the following steps −Create lists for labels, men_means and women_means with different data elements.Return evenly spaced values within a given interval, using numpy.arrange() method.Set the width variable, i.e., width=0.35.Create fig and ax variables using subplots method, where default nrows and ncols are 1.The bars are positioned at *x* with the given *align*\ment. Their dimensions are given by *height* and *width*. The vertical baseline is *bottom* (default 0), so create rect1 and rect2 using plt.bar() method.Set the Y-Axis label using plt.ylabel() method.Set a title for the axes using set_title() method.Get or set the current tick locations and ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
349 Views
To rotate xticklabels in matplotlib to make equal spacing between two xticklabels, we can take the following steps −Make a list of numbers from 1 to 4.Using subplot(), sdd a subplot to the current figure.Add xticks and yticks on the current subplot (using step 1).Set xtick labels by passing a list and to make label rotation (= 45).To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_yticks(x) ax1.set_xticklabels(["one", "two", "three", "four"], rotation=45) plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
994 Views
To plot a colorplot of a 2D array, we can take the following steps −Create data (i.e., 2D array) using numpy.For colorplot, use imshow() method, with input data (Step 1) and colormap is "PuBuGn".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, cmap='PuBuGn') plt.show()Output
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To adjust gridlines and ticks in matplotlib imshow(), we can take the following steps−Create data, a 2D array, using numpy.Using imshow() method, display data as an image.Set xticks and yticks using set_xticks and set_yticks method.To set the xticklabels and yticklabels, use set_xticklabels and set_yticklabels method.Lay out a grid in current line style. Supply the list of x an y positions using grid() 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 data = np.random.rand(9, 9) plt.imshow(data, interpolation="nearest") ax = plt.gca() ax.set_xticks(np.arange(-.5, 9, 1)) ax.set_yticks(np.arange(-.5, 9, 1)) ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
12K+ Views
To plot lines with colors through colormap, we can take the following steps−Create x and y data points using numpyPlot x and y data points using plot() method.Count n finds, number of color lines has to be plotted.Iterate in a range (n) and plot the lines.Limit the x ticks range.Use show() method to display the figure.Exampleimport numpy as np import matplotlib.pylab as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 2 * np.pi, 64) y = np.exp(x) plt.plot(x, y) n = 20 colors = plt.cm.rainbow(np.linspace(0, 1, n)) for i in range(n): plt.plot(x, i * y, color=colors[i]) plt.xlim(4, ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
506 Views
To plot a confusion matrix with string axis rather than integer in Python, we can take the following steps−Make a list for labels.Create a confusion matrix. Use confusion_matrix() to calculate accuracy of classification.3. Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Plot the values of a 2D matrix or array as a color-coded image.Using colorbar() method, create a colorbar for a ScalarMappable instance, *mappable*6. Set x and y ticklabels using set_xticklabels and set_yticklabels methods.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt from sklearn.metrics import confusion_matrix plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True labels ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
1K+ Views
To hide the axes in matplotlib 3D, we can take the following steps−Create a 2D array, where x, y, z, u, v and w are the coordinates of the arrow locations and direction components of the arrow vectors.Using figure() method, create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, using add_subplot() methodPlot a 3D field of arrows, using quiver() method.Using ylim, xlim, zlim, limit the range of the axesSet the title of the plot.Create two axes (ax1 and ax2). Set the titles "With Axes" and "Without Axes". Using set_axis_off() ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
4K+ Views
To add caption below X-axis for a scatter plot, we can use text() method for the current figure.StepsCreate x and y data points using numpy.Create a new figure or activate an existing figure using figure() method.Plot the scatter points with x and y data points.To add caption to the figure, use text() method.Adjust the padding between and around the subplots.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.random.rand(10) y = np.random.rand(10) fig = plt.figure() plt.scatter(x, y, c=y) fig.text(.5, .0001, "Scatter Plot", ha='center') plt.tight_layout() plt.show()OutputRead More