Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Matplotlib - Axes Class
Next article icon

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.

Python
importmatplotlib.pyplotaspltx=[1,2,3,4]y=[1,4,9,16]plt.plot(x,y)plt.show()

Output

Line_plot
Plotting a line graph using pyplot

Explanation:

  • 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.

Python
importmatplotlib.pyplotaspltlabels=['Python','Java','C++','JavaScript']sizes=[40,30,20,10]plt.pie(sizes,labels=labels,autopct='%1.1f%%')plt.show()

Output

Screenshot-2024-12-19-130530
Pie chart using pyplot

Explanation: 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.

Python
importmatplotlib.pyplotaspltcategories=['A','B','C','D']values=[3,7,2,5]plt.bar(categories,values)plt.show()

Output

Screenshot-2024-12-19-130702
Bar Chart using Pyplot

Explanation: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.

Python
importmatplotlib.pyplotaspltx=[1,2,3,4,5]y=[5,7,9,11,13]plt.scatter(x,y)plt.show()

Output

Screenshot-2024-12-19-130944
Scatter Plot using Pyplot

Explanation: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.

Python
importmatplotlib.pyplotaspltimportnumpyasnpdata=np.random.randn(1000)plt.hist(data,bins=30)plt.show()

Output

Screenshot-2024-12-19-131229
Histogram Using Pyplot

Explanation:

  • 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:


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp