# 输入数据
labels =['A','B','C']
values =[1,4,2]# 基本绘图
bars = plt.bar(labels,values)# 高阶的奇葩设置#bars[0].set_hatch('/')#bars[1].set_hatch('o')#bars[2].set_hatch('*')
patterns =['/','o','*']for bar in bars:
bar.set_hatch(patterns.pop(0))# 这种方式更高级了一点# 乱七八糟的设置
plt.title("My Second Graph!",fontdict={'fontsize':20})
plt.legend()
plt.show()
实例教学一:汽油价格
gas = pd.read_csv('./matplotlib_tutorial_download/gas_prices.csv')
gas.head()
Year
Australia
Canada
France
Germany
Italy
Japan
Mexico
South Korea
UK
USA
0
1990
NaN
1.87
3.63
2.65
4.59
3.16
1.00
2.05
2.82
1.16
1
1991
1.96
1.92
3.45
2.90
4.50
3.46
1.30
2.49
3.01
1.14
2
1992
1.89
1.73
3.56
3.27
4.53
3.58
1.50
2.65
3.06
1.13
3
1993
1.73
1.57
3.41
3.07
3.68
4.16
1.56
2.88
2.84
1.11
4
1994
1.84
1.45
3.59
3.52
3.70
4.36
1.48
2.87
2.99
1.11
plt.plot(gas.Year,gas.UK,'r-.')
plt.plot(gas.Year,gas.Japan,'b-.')
plt.title("Gas Prices Over Time (in USD)")# plt.xticks(range(1990,2009,1),rotation=50) # 这个是一种方法,下面有更好的方法
plt.xticks(gas.Year,rotation=50)# 更好更通用的方法
plt.yticks(range(0,9,1))
plt.xlabel("Year")
plt.ylabel("Gas Price")
plt.grid()
plt.legend()
plt.show()
plt.plot(gas.Year,gas.UK,'r-.')
plt.plot(gas.Year,gas.Japan,'b-.')
plt.title("Gas Prices Over Time (in USD)")
plt.xticks(gas.Year[::3])# 每三年来一次进行显示,显示效果更好哈哈哈
plt.yticks(range(0,9,1))
plt.xlabel("Year")
plt.ylabel("Gas Price")
plt.grid()
plt.legend()
plt.show()
# 这里面的逻辑是什么啊??????其实没有看懂for country in gas:if country !='Year':print(country)
Australia
Canada
France
Germany
Italy
Japan
Mexico
South Korea
UK
USA
# 把上面的语句用于我们的画图过程中# 画出所有的国家的油价
plt.figure(figsize=(7,5),dpi=500)for country in gas:if country !='Year':
plt.plot(gas.Year,gas[country],marker='.')# 注意这条语句的使用
plt.title("Gas Price over Time",fontdict={'fontweight':'bold','fontsize':22})
plt.xlabel("Year")
plt.ylabel("Gas Price")
plt.xticks(gas.Year[::3].tolist()+[2011])
plt.yticks(range(0,15,1))
plt.legend()
plt.savefig('GasPrice.png',dpi=300)
plt.show()
<matplotlib.figure.Figure at 0x7f0300059780>
实例教学二:FIFA球队
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fifa = pd.read_csv('./matplotlib_tutorial_download/fifa_data.csv')
fifa.head()
Unnamed: 0
ID
Name
Age
Photo
Nationality
Flag
Overall
Potential
Club
...
Composure
Marking
StandingTackle
SlidingTackle
GKDiving
GKHandling
GKKicking
GKPositioning
GKReflexes
Release Clause
0
0
158023
L. Messi
31
https://cdn.sofifa.org/players/4/19/158023.png
Argentina
https://cdn.sofifa.org/flags/52.png
94
94
FC Barcelona
...
96.0
33.0
28.0
26.0
6.0
11.0
15.0
14.0
8.0
€226.5M
1
1
20801
Cristiano Ronaldo
33
https://cdn.sofifa.org/players/4/19/20801.png
Portugal
https://cdn.sofifa.org/flags/38.png
94
94
Juventus
...
95.0
28.0
31.0
23.0
7.0
11.0
15.0
14.0
11.0
€127.1M
2
2
190871
Neymar Jr
26
https://cdn.sofifa.org/players/4/19/190871.png
Brazil
https://cdn.sofifa.org/flags/54.png
92
93
Paris Saint-Germain
...
94.0
27.0
24.0
33.0
9.0
9.0
15.0
15.0
11.0
€228.1M
3
3
193080
De Gea
27
https://cdn.sofifa.org/players/4/19/193080.png
Spain
https://cdn.sofifa.org/flags/45.png
91
93
Manchester United
...
68.0
15.0
21.0
13.0
90.0
85.0
87.0
88.0
94.0
€138.6M
4
4
192985
K. De Bruyne
27
https://cdn.sofifa.org/players/4/19/192985.png
Belgium
https://cdn.sofifa.org/flags/7.png
91
92
Manchester City
...
88.0
68.0
58.0
51.0
15.0
13.0
5.0
10.0
13.0
€196.4M
5 rows × 89 columns
画直方图
bins = np.arange(40,110,10)#plt.hist(fifa.Overall,bins=bins,color='#A87632') # 这里的颜色可以在浏览器搜索颜色选择器进行选择,然后copy HEX code就好了
plt.hist(fifa.Overall,bins=bins,color='#000000')# 黑色
plt.xticks(bins)
plt.yticks(np.arange(0,11000,1000))
plt.xlabel("Skill levels")
plt.ylabel("Number of Players")
plt.title("Distribution of Player Skills in FIFA 2018")
plt.show()
饼图 I
fifa['Preferred Foot']
0 Left
1 Right
2 Right
3 Right
4 Right
5 Right
6 Right
7 Right
8 Right
9 Right
10 Right
11 Right
12 Right
13 Left
14 Right
15 Left
16 Right
17 Left
18 Right
19 Left
20 Right
21 Right
22 Right
23 Right
24 Left
25 Right
26 Left
27 Right
28 Left
29 Right
...
18177 Right
18178 Right
18179 Right
18180 Right
18181 Right
18182 Right
18183 Right
18184 Right
18185 Right
18186 Right
18187 Right
18188 Right
18189 Right
18190 Right
18191 Left
18192 Right
18193 Right
18194 Right
18195 Right
18196 Right
18197 Right
18198 Right
18199 Right
18200 Left
18201 Left
18202 Right
18203 Right
18204 Right
18205 Right
18206 Right
Name: Preferred Foot, Length: 18207, dtype: object
left = fifa.loc[fifa['Preferred Foot']=='Left'].count()[0]print(left)
right = fifa.loc[fifa['Preferred Foot']=='Right'].count()[0]print(right)
4211
13948
labels =['Left','Right']
colors =['#abcdef','#aabbcc']
plt.pie([left,right],labels=labels,colors=colors,autopct='%.2f %%')# 注意这里面是个方括号
plt.title("Foot Preference of FIFA players")
plt.show()
# 去掉这个磅的符号,并且变成一个浮点数
fifa.Weight =[int(x.strip('lbs'))iftype(x)==strelse x for x in fifa.Weight]
fifa.Weight[0]
159.0
light = fifa.loc[fifa.Weight<125].count()[0]print(light)
light_medium = fifa.loc[(fifa.Weight>=125)&(fifa.Weight<150)].count()[0]print(light_medium)
medium = fifa.loc[(fifa.Weight>=150)&(fifa.Weight<175)].count()[0]print(medium)
medium_heavy = fifa.loc[(fifa.Weight>=175)&(fifa.Weight<200)].count()[0]print(medium_heavy)
heavy = fifa.loc[fifa.Weight>=200].count()[0]print(heavy)
plt.figure(figsize=(5,5),dpi=100)#plt.style.use('default')
plt.style.use('ggplot')# 直接用其他的一些风格
weights =[light,light_medium,medium,heavy,medium_heavy]
labels =['light','light_medium','medium','heavy','medium_heavy']
explode =[.4,.1,.1,.4,.1]# 让两个少的往外一点点
plt.title("Weight Distrubution of FIFA Players (in lbs)")
plt.pie(weights,labels=labels,autopct="%.2f %%",explode=explode)
plt.show()
41
2290
10876
4583
369
画箱形图
fifa.head()
Unnamed: 0
ID
Name
Age
Photo
Nationality
Flag
Overall
Potential
Club
...
Composure
Marking
StandingTackle
SlidingTackle
GKDiving
GKHandling
GKKicking
GKPositioning
GKReflexes
Release Clause
0
0
158023
L. Messi
31
https://cdn.sofifa.org/players/4/19/158023.png
Argentina
https://cdn.sofifa.org/flags/52.png
94
94
FC Barcelona
...
96.0
33.0
28.0
26.0
6.0
11.0
15.0
14.0
8.0
€226.5M
1
1
20801
Cristiano Ronaldo
33
https://cdn.sofifa.org/players/4/19/20801.png
Portugal
https://cdn.sofifa.org/flags/38.png
94
94
Juventus
...
95.0
28.0
31.0
23.0
7.0
11.0
15.0
14.0
11.0
€127.1M
2
2
190871
Neymar Jr
26
https://cdn.sofifa.org/players/4/19/190871.png
Brazil
https://cdn.sofifa.org/flags/54.png
92
93
Paris Saint-Germain
...
94.0
27.0
24.0
33.0
9.0
9.0
15.0
15.0
11.0
€228.1M
3
3
193080
De Gea
27
https://cdn.sofifa.org/players/4/19/193080.png
Spain
https://cdn.sofifa.org/flags/45.png
91
93
Manchester United
...
68.0
15.0
21.0
13.0
90.0
85.0
87.0
88.0
94.0
€138.6M
4
4
192985
K. De Bruyne
27
https://cdn.sofifa.org/players/4/19/192985.png
Belgium
https://cdn.sofifa.org/flags/7.png
91
92
Manchester City
...
88.0
68.0
58.0
51.0
15.0
13.0
5.0
10.0
13.0
€196.4M
5 rows × 89 columns
barcelona = fifa.loc[fifa.Club=='FC Barcelona']['Overall']
madrid = fifa.loc[fifa.Club=='Real Madrid']['Overall']
revs = fifa.loc[fifa.Club=='New England Revolution']['Overall']
labels =['FC Barcelona','Real Madrid','New England Revolution']
plt.style.use('default')
plt.figure(figsize=(7,10))
boxes = plt.boxplot([barcelona, madrid, revs],labels=labels, patch_artist=True)#设置这个True的原因是后面要设置facecolor for box in boxes['boxes']:# 方括号内的是参数# 设置box边界的颜色
box.set(color='#4286f4',linewidth=2)# 设置box内部颜色
box.set(facecolor='#e0e0e0')
plt.title("Professional Soccer Team Comparison")
plt.ylabel("FIFA Overall Rating")
plt.show()
/home/chen/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py:57: FutureWarning: reshape is deprecated and will raise in a subsequent release. Please use .values.reshape(...) instead
return getattr(obj, method)(*args, **kwds)