程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pandas data visualization

編輯:Python

List of articles

  • 1、 install Matplotlib
  • 2、 Broken line diagram
    • 1)2010 year ~2022 Data of college graduates in
    • 2)2010 year ~2022 Annual Graduate data
    • 3) Change the color 、 Line thickness
    • 4) Polyline 4 Styles

When we analyze data , If there is only a pile of data in front of you , You must look uncomfortable . It is better to change the data , Present through graphics , After realizing data visualization , We can be faster 、 More easily 、 Understand the data more clearly 、 Message conveyed .

Python In terms of data presentation , There are a lot of good tools , such as Matplotlib、Seaborn、Pygal etc. , Are popular feature packs . So let's see , How to display local data , And the data obtained from the network .

Visual view , Generally speaking, we can divide it into 4 Categories: , Namely :

  • Compare with each other : For example, line chart , You can compare the relationship between various categories of data , Including the changing trend of data over time ;
  • Connect with each other : For example, scatter chart , The relationship between two or more instances can be observed ;
  • Composition proportion : Like pie chart , You can intuitively see the proportion of each part 、 Share size , Including its proportion change with time ;
  • Distribution situation : Like histogram , You can observe the specific distribution of single or multiple variables .

frequently-used 10 Species view : Scatter plot 、 Broken line diagram 、 Histogram 、 Bar chart 、 The pie chart 、 Heat map 、 Box figure 、 Spiders 、 Binary variable distribution diagram 、 In pairs .

1、 install Matplotlib

Open the terminal , Input pip install matplotlib Can be installed automatically .

If you want to see Matplotlib Developer documentation , Input python -m pydoc -p 8899 that will do , Visit after startup http://localhost:8899, stay .../site-packages Found under column matplotlib(package) Is it .

2、 Broken line diagram

Have you seen the title again recently 「 The worst graduation season in history 」 My article ? Actually , It's written like this every year , Every year is the most difficult 、 The worst .

Actually , We can find the corresponding data , Draw it as a line chart , You can see the trend of the number of graduates .

This is a 2010 year ~2022 Data of college graduates in ( Company : ten thousand ):

year Number of college graduates ( Company : ten thousand ) Number of graduate students 202210761202021909117.652020874110.66201983491.65201882185.8201779580.61201676566.71201574964.51201470062.13201369961.14201268058.97201166056.02201063153.82

1)2010 year ~2022 Data of college graduates in

According to the data given above , Let's draw a line chart first , Let's see 2010 Year to 2022 The changing trend of the number of college graduates between . among , We X The axis is the year ,Y Number of people on axis , The code is as follows :

# Definition X Axis and Y Axis data 
# among ,X The axis is the year ;Y The axis is the number of graduates ( Company : ten thousand )
xData = [2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022]
yData = [631,660,680,699,700,749,765,795,821,834,874,909,1076]
# Parameters 1, Set the value of abscissa 
# Parameters 2, Set the value of the vertical coordinate 
plt.plot(xData, yData, xData, yData2)
# Show the image 
plt.show()

Running results , As shown in the figure :

2)2010 year ~2022 Annual Graduate data

Well, if you say , I want to see if the number of graduate students has also increased so much , So exaggerated ? It's not hard , Pass in multiple delegates X Axis 、Y Just list the axis data , You can get a composite line chart .

The code is as follows :

# Definition X Axis and Y Axis data 
# among ,X The axis is the year ;Y The axis is the number of graduates ( Company : ten thousand )
xData = [2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022]
yData = [631,660,680,699,700,749,765,795,821,834,874,909,1076]
# Number of graduate students 
yData2 = [53.82,56.02,58.97,61.14,62.13,64.51,66.71,80.61,85.8,91.65,110.66,117.65,120]
# Parameters 1, Set the value of abscissa 
# Parameters 2, Set the value of the vertical coordinate 
# Parameters 3, Set the value of the abscissa of the second polyline 
# Parameters 4, Set the value of the vertical coordinate of the second polyline 
plt.plot(xData, yData, xData, yData2)
# Show the image 
plt.show()

Running results , As shown in the figure :

3) Change the color 、 Line thickness

even to the extent that , You can also change the color of the broken line 、 thickness , It's also very simple. . such as , adopt color You can specify the corresponding color , adopt linewidth You can specify the thickness of the polyline , The code is as follows :

# adopt color You can specify the corresponding color 
# adopt linewidth You can specify a thickness value 
plt.plot(xData, yData, color='orange', linewidth=5.0)
plt.plot(xData, yData2, color='green', linewidth=5.0)
# Show the image 
plt.show()

Running results , As shown in the figure :

4) Polyline 4 Styles

If you don't like it 「 Solid line 」 Lines of , You can also pass linestyle To change the , There are four common types , Namely :

The first one is ,- Said solid line ( The default value is );

The second kind :-- Said the dotted line ;

The third kind of :: Represents an imaginary point ;

A fourth :-. Indicates a short line 、 The combination of points .

The code is as follows :

plt.plot(xData, yData, color='orange', linewidth=5.0, linestyle='--')
plt.plot(xData, yData2, color='green', linewidth=5.0, linestyle='-.')
# Show the image 
plt.show()

Running results , As shown in the figure :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved