Movatterモバイル変換


[0]ホーム

URL:


Open In App

We have aPandas DataFrame and now we want to visualize it usingMatplotlib for data visualization to understand trends, patterns and relationships in the data. In this article we will explore different ways to plot a Pandas DataFrame using Matplotlib's various charts. Before we start, ensure you have the necessary libraries using:

pip install pandas matplotlib

Types of Data Visualizations in Matplotlib

Matplotlib offers a wide range of visualization techniques that can be used for different data types and analysis. Choosing the right type of plot is essential for effectively conveying data insights.

1. Bar Plot

Bar Plot is useful for comparing values across different categories and comparingCategorical Data. It displays rectangular bars where the height corresponds to the magnitude of the data.

Python
importpandasimportmatplotlibdata={'Category':['A','B','C','D'],'Values':[20,34,30,35]}df=pd.DataFrame(data)plt.bar(df['Category'],df['Values'],color='skyblue')plt.xlabel("Category")plt.ylabel("Values")plt.title("Bar Chart Example")plt.show()

Output:

bar
Bar Plot

2. Histogram

A histogram groups numerical data into intervals (bins) to show frequency distributions. It helps in understanding data distribution and spotting trends and is widely used for continuous data.

Python
importpandasimportmatplotlibdf=pd.DataFrame({'Values':[12,23,45,36,56,78,89,95,110,130]})plt.hist(df['Values'],bins=5,color='lightcoral',edgecolor='black')plt.xlabel("Value Range")plt.ylabel("Frequency")plt.title("Histogram Example")plt.show()

Output:

hist
Histogram

3. Pie Chart

Apie chart is used to display categorical data as proportions of a whole. Each slice represents a percentage of the dataset.

Python
importpandasimportmatplotlibdf=pd.DataFrame({'Category':['A','B','C'],'Values':[30,50,20]})plt.pie(df['Values'],labels=df['Category'],autopct='%1.1f%%',colors=['gold','lightblue','pink'])plt.title("Pie Chart Example")plt.show()

Output:

pie
Pie Chart

4. Scatter Plot

A scatter plot visualizes the relationship between two numerical variables helping us to identify correlations and data patterns.

Python
importpandasimportmatplotlibdf=pd.DataFrame({'X':[1,2,3,4,5],'Y':[2,4,1,8,7]})plt.scatter(df['X'],df['Y'],color='green',marker='o')plt.xlabel("X-axis")plt.ylabel("Y-axis")plt.title("Scatter Plot Example")plt.show()

Output:

scatter
Scatter Plot

5. Line Chart

A line chart is ideal for analyzing trends and patterns in time-series or sequential data by connecting data points with a continuous line.

Python
importpandasimportmatplotlibdf=pd.DataFrame({'Month':['Jan','Feb','Mar','Apr','May'],'Sales':[200,250,300,280,350]})plt.plot(df['Month'],df['Sales'],marker='o',linestyle='-',color='blue')plt.xlabel("Month")plt.ylabel("Sales")plt.title("Line Chart Example")plt.grid()plt.show()

Output:

line
Line Chart

In this article we explored various techniques to visualize data from a Pandas DataFrame using Matplotlib. From bar charts for categorical comparisons to histograms for distribution analysis and scatter plots for identifying relationships each visualization serves a unique purpose.


Improve

Explore

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