Matplotlib's pyplot module is a widely used interface that simplifies the process of creating visualizations in Python. It provides a MATLAB-like syntax, allowing users to generate line charts, bar graphs, scatter plots and other common visualizations with ease. With just a few lines of code analysts and developers can turn raw data into clear, informative charts for analysis and presentation.
This article will help you understand how to use Matplotlib’s Pyplot module to create simple charts.
Basic Plotting with Pyplot
Below are some of the most commonly used chart types in Pyplot, each demonstrated with a short example.
1. Line Plot
Line plots are simplest types of charts. Theplot() function takes two lists: one for x-axis and one for y-axis and connects the points in a linear path.
Example:
Following code usesplt.plot() to draw a basic line graph for the givenxandyvalues using Pyplot module.
Pythonimportmatplotlib.pyplotaspltx=[1,2,3,4]y=[1,4,9,16]plt.plot(x,y)plt.show()
Output
Plotting a line graph using pyplotExplanation:
- plt.plot(x, y):creates a line plot by connectingxandydata points.
- plt.show(): displays final plot window.
2. Pie Charts
Pie charts show the share of each category in a whole. Thepie() function takes a list of values and optional labels to represent each slice of the pie.
Example:
This code creates a pie chart usingplt.pie()function to visualize the percentage share of different programming languages.
Pythonimportmatplotlib.pyplotaspltlabels=['Python','Java','C++','JavaScript']sizes=[40,30,20,10]plt.pie(sizes,labels=labels,autopct='%1.1f%%')plt.show()
Output
Pie chart using pyplotExplanation: plt.pie(sizes, labels=labels, autopct='%1.1f%%')creates a pie chart withslice sizes from sizes, labelsfor each sliceandautopctshows percentage values with one decimal place.
3. Bar Charts
Bar charts are ideal for comparing quantities across categories. The bar() function takes two lists one forcategorylabels (x-axis) and one for their correspondingvalues(y-axis).
Example:
This Example creates a bar chart usingbar() function to display values for different categories.
Pythonimportmatplotlib.pyplotaspltcategories=['A','B','C','D']values=[3,7,2,5]plt.bar(categories,values)plt.show()
Output
Bar Chart using PyplotExplanation:plt.bar(categories, values) draws a vertical bar chart using given categories and values.
4. Scatter Plots
Scatter plots are used for displaying individual data points and showing relationship between two variables. The scatter() function takes two lists like plot(), but it only plots the individual points without connecting them.
Example:
Here, scatter plot is plotted usingscatter() function to visualize individual data points.
Pythonimportmatplotlib.pyplotaspltx=[1,2,3,4,5]y=[5,7,9,11,13]plt.scatter(x,y)plt.show()
Output
Scatter Plot using PyplotExplanation:plt.scatter(x, y) plots individual data points as dots at specified(x, y) coordinates.
5. Histograms
Histograms are used to visualize distribution of a dataset. Thehist() function takes a list or array of numerical data and divides it into bins.
Example:
A histogram is plotted usinghist() function to visualize the distribution of 1000 random data points.
Pythonimportmatplotlib.pyplotaspltimportnumpyasnpdata=np.random.randn(1000)plt.hist(data,bins=30)plt.show()
Output
Histogram Using PyplotExplanation:
- np.random.randn(1000): generates1000random numbers from a standard normal distribution.
- plt.hist(data, bins=30): creates a histogram of the data with 30 bins (intervals).
Related Articles: