操作举例
In [2]: import matplotlib.pyplot as plt
In [3]: import numpy as np
In [4]: import pandas as pd
In [5]: df=pd.read_csv('/Users/jiaxiaolei/Desktop/london.csv')
In [6]: df
Out[6]:
Year Month Tmax Tmin Rain Sun
0 2018 1 9.7 3.8 58 46.5
1 2018 2 6.7 0.6 29 92.0
2 2018 3 6.7 0.6 29 92.0
3 2018 4 6.7 0.6 29 92.0
4 2018 5 6.7 0.6 29 92.0
5 2018 6 6.7 0.6 29 92.0
6 2018 7 6.7 0.6 29 92.0
7 2018 8 6.7 0.6 29 92.0
8 2018 9 6.7 0.6 29 92.0
9 2018 10 6.7 0.6 29 92.0
10 2018 11 6.7 0.6 29 92.0
11 2018 12 6.7 0.6 29 92.0
# 读取excel
In [39]: df=pd.read_excel('/Users/jiaxiaolei/Desktop/london.xls')
In [40]: df
Out[40]:
Year Month Tmax Tmin Rain Sun
0 2018 1 9.7 3.8 58 46.5
1 2018 2 6.7 0.6 29 92.0
2 2018 3 6.7 0.6 29 92.0
3 2018 4 6.7 0.6 29 92.0
4 2018 5 6.7 0.6 29 92.0
5 2018 6 6.7 0.6 29 92.0
6 2018 7 6.7 0.6 29 92.0
7 2018 8 6.7 0.6 29 92.0
8 2018 9 6.7 0.6 29 92.0
9 2018 10 6.7 0.6 29 92.0
10 2018 11 6.7 0.6 29 92.0
11 2018 12 6.7 0.6 29 92.0
In [7]: df.plot(x='Month',y='Tmax')
Out[7]: <matplotlib.axes._subplots.AxesSubplot at 0x11730f160>
In [8]: plt.show()
In [7]: df.plot(x='Month',y='Tmax', kind='line', grid=True)
Out[7]: <matplotlib.axes._subplots.AxesSubplot at 0x11d89f208>
In [8]: plt.show()
In [9]: df.plot(x='Month',y=['Tmax', 'Tmin'], kind='line', grid=True)
Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0x11c8264e0>
In [10]: plt.show()
In [11]: df.plot(x='Month',y=['Tmax', 'Tmin'], kind='bar', grid=True)
Out[11]: <matplotlib.axes._subplots.AxesSubplot at 0x11d90b4e0>
In [12]: plt.show()
In [13]: df.plot(x='Month',y=['Tmax', 'Tmin'], kind='barh', grid=True)
Out[13]: <matplotlib.axes._subplots.AxesSubplot at 0x11d9bf908>
In [14]: plt.show()
# 散点图
In [20]: df.plot(x='Month',y=['Tmax'], kind='scatter')
Out[20]: <matplotlib.axes._subplots.AxesSubplot at 0x11dc05128>
In [21]: plt.show()
#NOTE:
这里使用 In [23]: df.plot(x='Month',y=['Tmax', 'Tmin'], kind='scatter') 多个纵轴 出现错误。
ValueError: x and y must be the same size
# 饼状图:
In [26]: df.plot(x='Month',y=['Sun'], kind='pie')
Out[26]: <matplotlib.axes._subplots.AxesSubplot at 0x122a3f710>
In [27]: plt.show()
# 关闭竖状图例
In [28]: df.plot(x='Month',y=['Sun'], kind='pie', legend=False)
Out[28]: <matplotlib.axes._subplots.AxesSubplot at 0x1203d6fd0>
In [29]: plt.show()
# 使用自定义的index
In [31]: df.index=['Jan','Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
In [32]: df.plot(x='Month',y=['Sun'], kind='pie', legend=False)
Out[32]: <matplotlib.axes._subplots.AxesSubplot at 0x120a84d30>
In [33]: plt.show()
扩展阅读
使用pandas做数据可视化
https://mp.weixin.qq.com/s/d_EAPkeTPcnaSPPYS37qrg
简介:
示例很好...
实操性也比较好
网友评论