- 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)
2K+ Views
To convert a .wav file to a spectrogram in python3, we can take the following steps −Load a .wav file from local machine.Compute a spectrogram with consecutive Fourier transforms using spectrogram() method.Create a pseudocolor plot with a non-regular rectangular grid using pcolormesh() method.Use imshow() method with spectrogram.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt from scipy import signal from scipy.io import wavfile plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True sample_rate, samples = wavfile.read('test.wav') frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate) plt.pcolormesh(times, frequencies, spectrogram, shading='flat') plt.imshow(spectrogram) plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
4K+ Views
To draw axis lines or the origin for matplotlib contour plot, we can use contourf(), axhline() y=0 and axvline() x=0.Create data points for x, y, and z using numpy.To set the axes properties, we can use plt.axis('off') method.Use contourf() method with x, y, and z data points.Plot x=0 and y=0 lines with red color.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 x = np.linspace(-1.0, 1.0, 10) x, y = np.meshgrid(x, x) z = -np.hypot(x, y) plt.axis('off') plt.contourf(x, y, z, 10) plt.axhline(0, color='red') plt.axvline(0, color='red') plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
7K+ Views
To plot a line graph from histogram data in matplotlib, we use numpy histogram method to compute the histogram of a set of data.StepsAdd a subplot to the current figure, nrows=2, ncols=1 and index=1.Use numpy histogram method to get the histogram of a set of data.Plot the histogram using hist() method with edgecolor=black.At index 2, use the computed data (from numpy histogram). To plot them, we can use plot() 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 plt.subplot(211) data = np.array(np.random.rand(100)) y, binEdges = np.histogram(data, bins=100) plt.hist(data, bins=100, edgecolor='black') ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
2K+ Views
To plot multi-colored lines, like a rainbow, we can create a list of seven rainbow colors (VIBGYOR).StepsCreate x for data points using numpy.Create a list of colors (rainbow VIBGYOR).Iterate in the range of colors list length.Plot lines with x and y(x+i/20) using plot() method, with marker=o, linewidth=7 and colors[i] where i is the index.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) colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] for i in range(len(colors)): plt.plot(x, x+i/20, c=colors[i], lw=7, marker='o') plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
2K+ Views
To remove the label on the left side in a matplotlib pie chart, we can take the following steps −Create lists of hours, activities, and colors.Plot a pie chart using pie() method.To hide the label on the left side in matplotlib, we can use plt.ylabel("") with ablank string.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True hours = [8, 1, 11, 4] activities = ['sleeping', 'exercise', 'studying', 'working'] colors = ["grey", "green", "orange", "blue"] plt.pie(hours, labels=activities, colors=colors, autopct="%.2f") plt.ylabel("") plt.show()Output
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To animate the line plot in matplotlib, we can take the following steps −Create a figure and a set of subplots using subplots() method.Limit x and y axes scale.Create x and t data points using numpy.Return coordinate matrices from coordinate vectors, X2 and T2.Plot a line with x and F data points using plot() method.To make animation plot, update y data.Make an animation by repeatedly calling a function *func*, current fig, animate, and interval.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 = ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
3K+ Views
To display text over columns in a bar chart, we can use text() method so that we could place text at a specific location (x and y) of the bars column.StepsCreate lists for x, y and percentage.Make a bar plot using bar() method.Iterate zipped x, y and percentage to place text for the bars column.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 = ['A', 'B', 'C', 'D', 'E'] y = [1, 3, 2, 0, 4] percentage = [10, 30, 20, 0, 40] ax = plt.bar(x, y) for x, y, p in zip(x, y, percentage): ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
925 Views
To handle an asymptote/discontinuity with matplotlib, we can take the following steps −Create x and y data points using numpy.Turn off the axes plot.Plot the line with x and y data points.Add a horizontal line across the axis, x=0.Add a vertical line across the axis, y=0.Place legend for the curve y=1/x.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 = 1 / x plt.axis('off') plt.plot(x, y, label='y=1/x') plt.axhline(y=0, c='red') plt.axvline(x=0, c='red') plt.legend(loc='upper left') plt.show()OutputRead More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
7K+ Views
To show multiple images in one figure in matplotlib, we can take the following steps −Create random data using numpy.Add a subplot to the current figure, nrows=1, ncols=4 and at index=1.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Blues_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=2.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="Accent_r".Add a subplot to the current figure, nrows=1, ncols=4 and at index=3.Display data as an image, i.e., on a 2D regular raster, using imshow() method with cmap="terrain_r".Add a subplot to the current figure, nrows=1, ... Read More
![Rishikesh Kumar Rishi](https://www.tutorialspoint.com/assets/profiles/318007/profile/60_254496-1615815423.jpg)
8K+ Views
To remove gaps between bars, we can change the align value to center in the argument of bar() method.StepsCreate a dictionary called data with two keys, milk and water.Get the list of keys and values in the dictionay.Using subplots() method, create a figure and add a set of two subplots.On axis 2, use bar method to plot bars without gaps. Set the width attribute as 1.0. Set the title using set_title() method.Use tight_layout() to adjust the padding between and around the subplots.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 data = {'milk': 12, 'water': ... Read More