- 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 1030 Articles for Matplotlib

1K+ Views
In this program, we will plot two lines using the matplot library. Before starting to code, we need to first import the matplotlib library using the following command −Import matplotlib.pyplot as pltPyplot is a collection of command style functions that make matplotlib work like MATLAB.AlgorithmStep 1: Import matplotlib.pyplot Step 2: Define line1 and line2 points. Step 3: Plot the lines using the plot() function in pyplot. Step 4: Define the title, X-axis, Y-axis. Step 5: Display the plots using the show() function.Example Codeimport matplotlib.pyplot as plt line1_x = [10, 20, 30] line1_y = [20, 40, 10] line2_x = ... Read More

2K+ Views
Using pylab.gcf(), we can create a fig variable and can set the fig.canvas.set_window_title('Setting up window title.') window title.StepsUsing gcf() method, get the current figure. If no current figure exists, a new one is created using `~.pyplot.figure()`.Set the title text of the window containing the figure, using set_window_title() method.. Note that this has no effect if there is no window (e.g., a PS backend).ExamplePlease use Ipython and follow the steps given below -In [1]: from matplotlib import pylab In [2]: fig = pylab.gcf() In [3]: fig.canvas.set_window_title('Setting up window title.')OutputRead More

12K+ Views
Using plt.xticks, we can change the X-axis scale.StepsUsing plt.plot() method, we can create a line with two lists that are passed in its argument.Add text to the axes. Add the text *s* to the axes at location *x*, *y* in data coordinates, using plt.text() method, where the font size can be customized by changing the font-size value.Using xticks method, get or set the current tick locations and labels of the X-axis.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.plot([1, 2, 4], [1, 2, 4]) plt.text(2, 3, "y=x", color='red', fontsize=20) plt.xticks([1, 2, 3, 4, 5]) ... Read More

2K+ Views
Using plt.text() method, we can increase the font size.StepsUsing plt.plot() method, we can create a line with two lists that are passed in its argument.Add text to the axes. Add the text *s* to the axes at location *x*, *y* in data coordinates, using plt.text() method. Font size can be customized by changing the font-size value.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.plot([1, 2, 4], [1, 2, 4]) plt.text(2, 3, "y=x", color='red', fontsize=20) # Increase fontsize by increasing value. plt.show()Output

249 Views
We can use a user-defined method, autolabel, to annotate the axis value. Before that, we can initialize the fig and ax using plt.subplots() method.StepsCreate lists, 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 ... Read More

249 Views
In this program, we can create a data frame and can plot a bar using df.plot.bar(x='lab', y='value', color='#5fba34') plot.StepsUsing Panda’s dataframe, we can create a data frame with the given dictionary, where the keys are lab and value, values of these keys are lists, respectively.Using Pandas plot.bar() method, we can create a vertical bar plot. A bar plot is a plot that presents categorical data with rectangular bars with lengths proportional to the values that they represent.To show the figure, use the plt.show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt df = pd.DataFrame({'lab': ['A', 'B', 'C'], ... Read More

2K+ Views
Using plt.grid(axis="x") method, we can plot vertical gridlines.StepsMake a list of numbers.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Toggle the gridlines, and optionally set the properties of the lines, using plt.grid() method.To show the figure, use the plt.show() method, where the argument axis can be “x”, “y” or “both”.Examplefrom matplotlib import pyplot as plt plt.plot([0, 5], [0, 5]) plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.grid(axis="x") plt.show()Output

10K+ Views
We can use the resolution value, i.e., dots per inch, and the image format to plot a high-resolution graph in Matplotlib.StepsCreate a dictionary with Column 1 and Column 2 as the keys and Values are like i and i*i, where i is from 0 to 10, respectively.Create a data frame using pd.DataFrame(d); d created in step 1.Plot the data frame with ‘o’ and ‘rx’ style.To save the file in pdf format, use savefig() method where the image name is myImagePDF.pdf, format="pdf".We can set the dpi value to get a high-quality image.Using the saving() method, we can save the image with ... Read More

2K+ Views
To get interactive plots, we need to activate the figure. Using plt.ioff() and plt.ion(), we can perform interactive actions with plot.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Draw a line, using plot() method.Set the color of the line, i.e., orange.Stopped the interaction, using plt.ioff() method.To make the interaction plots, change the color of the line coordinate.Start the interaction, using plt.ion() method.ExampleTo use interactive plot in Ipython -In [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as plt In [3]: fig, ax = plt.subplots() # Diagram will ... Read More

24K+ Views
Using plt.savefig("myImagePDF.pdf", format="pdf", bbox_inches="tight") method, we can save a figure in PDF format.StepsCreate a dictionary with Column 1 and Column 2 as the keys and Values are like i and i*i, where i is from 0 to 10, respectively.Create a data frame using pd.DataFrame(d), d created in step 1.Plot the data frame with ‘o’ and ‘rx’ style.To save the file in PDF format, use savefig() method where the image name is myImagePDF.pdf, format = ”pdf”.To show the image, use the plt.show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt d = {'Column 1': [i for i in ... Read More