一、Matplotlib各种图形demo
1 | import pandas as pd |
线状图
1 | x = [1,2,3] |
条形图
1 | plt.bar([1,3,5,7,9],[5,2,7,8,2], label="Example one") |
直方图Histogram
1 | population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,110,120,121,122,130,111,115,112,80,75,65,54,44,43,42,48] |
散点图
1 | x = [1,2,3,4,5,6,7,8] |
堆叠图
1 | days = [1,2,3,4,5] |
饼图
1 | slices = [7,2,2,13] |
二、自定义绘图
1 | x=np.linspace(0,10,50) |
1 | plt.plot(x,np.sin(x),'--') |
1 | fig=plt.figure() |
1 | fig.savefig('/Users/iherr/Desktop/a.png') |
1 | plt.subplot(2,1,1) |
1 | # 点状样式,颜色 |
1 | # 图例 |
1 | x=np.linspace(0,10,20) |
1 | x=np.linspace(0,10,20) |
1 | #plt.style.use('classic') |
三、pandas自带绘画
线性图
1 | df2=pd.DataFrame(np.random.rand(100,4).cumsum(0),columns=['A','B','C','D']) |
1 | df2.plot() |
1 | df2.A.plot() |
1 | df3=pd.DataFrame(np.random.randint(10,50,(3,4)).cumsum(0),columns=['A','B','C','D'],index=['one','two','three']) |
A | B | C | D | |
---|---|---|---|---|
one | 35 | 10 | 45 | 14 |
two | 71 | 50 | 63 | 36 |
three | 83 | 62 | 111 | 81 |
柱状图
1 |
|
1 | #和上方等价 |
1 | #累加 |
1 | df3.A.plot.bar() |
直方图
1 | df4=pd.DataFrame(np.random.randn(100,4),columns=['A','B','C','D']) |
1 | df4.plot(kind='kde') |
1 |
|
1 | ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) |
https://zhuanlan.zhihu.com/p/197854613 Python-Matplotlib用户必备的画图速查表
地图
nbcharts.com
天地图
高德地图