其他
Matplotlib简单画图:(二)
版权声明:本文为CSDN博主「向前走别回头」与「墨岚❤️」的原创文章,遵循 CC 4.0 BY-SA 版权协议,特此附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/weixin_39778570/article/details/81139841
https://blog.csdn.net/weixin_39778570/article/details/81143763
https://blog.csdn.net/weixin_39778570/article/details/81144895
pandas绘图之Series
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
# cumsum()函数演示
s = Series([1,2,3,4,5])
s.cumsum()
0 1
1 3
2 6
3 10
4 15
dtype: int64
s1 = Series(np.random.randn(1000)).cumsum()
# kind参数是修改画图类型
s1[:10].plot(kind='bar')
# 设置title,grid网格线,还有线的类型style
s1.plot(kind='line', grid=True, label='S1',title='This is Series', style='--')
# 绘制两个Series
s1.plot(kind='line', grid=True, label='S1',title='This is Series', style='--')
s2 = Series(np.random.randn(1000)).cumsum()
s2.plot(label='S2')
plt.legend()
使用subplots
fig, ax = plt.subplots(2,1)
ax
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000001F875C276D8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x000001F875CAE400>], dtype=object)
s1.plot(ax=ax[0], label='S1')
<matplotlib.axes._subplots.AxesSubplot at 0x1f875c276d8>
s1.plot(ax=ax[1], label='S2')
<matplotlib.axes._subplots.AxesSubplot at 0x1f875cae400>
fig
fig, ax = plt.subplots(2,1)
s1[0:10].plot(ax=ax[0], kind='bar', label='S1')
s2.plot(ax=ax[1], label='S2')
import numpy as np
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
# 创建一个10行4列的DataFrame
df = DataFrame(
np.random.randint(1,10,40).reshape(10,4),
columns = {'A','B','C','D'}
)
df
B A D C
0 1 3 8 4
1 2 3 1 9
2 6 5 7 1
3 1 2 9 7
4 4 2 4 7
5 2 1 4 5
6 9 8 1 5
7 2 3 5 1
8 4 2 1 6
9 9 4 3 8
# kind='bar'是柱形图,默认为line
df.plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0x1ee7b401d30>
plt.show()
# 横柱形图
df.plot(kind='barh')
# stacked=True堆叠
df.plot(kind='bar',stacked=True)
plt.show()
df.plot(kind='area')
plt.show()
# 画一行
df.iloc[5].plot()
plt.show()
# 画10行
for i in df.index:
df.iloc[i].plot(label=str(i))
plt.legend()
plt.show()
# 画一列
df['A'].plot()
plt.show()
df.T
0 1 2 3 4 5 6 7 8 9
B 2 7 9 7 9 8 6 4 6 1
A 2 5 5 3 6 1 7 7 5 9
D 1 6 4 9 1 8 1 4 8 5
C 9 5 2 4 5 1 1 4 2 9
# 转置画行
df.T.plot()
plt.show()
直方图和密度图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
s = Series(np.random.randn(1000))
# 生成了两个array和一个图形对象,第一个array是在区间数的数量,第二个是区间范围
# hist画直方图,rwidth表示图形宽度
plt.hist(s, rwidth=0.9)
(array([ 6., 13., 56., 126., 208., 258., 181., 111., 24., 17.]),
array([-3.24627557, -2.64169276, -2.03710994, -1.43252712, -0.82794431,
-0.22336149, 0.38122132, 0.98580414, 1.59038695, 2.19496977,
2.79955259]),
<a list of 10 Patch objects>)
plt.show()
左右滑动查看更多
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
plt.hist(a, rwidth=0.9)
左右滑动查看更多
# bins默认10,分割区间,orientation修改为水平的,color修改颜色,
plt.hist(s, rwidth=0.9, bins=20, color='r', orientation='horizontal')
(array([ 3., 3., 2., 11., 27., 29., 52., 74., 90.,
118., 132., 126., 99., 82., 69., 42., 11., 13.,
8., 9.]),
array([-3.24627557, -2.94398416, -2.64169276, -2.33940135, -2.03710994,
-1.73481853, -1.43252712, -1.13023572, -0.82794431, -0.5256529 ,
-0.22336149, 0.07892992, 0.38122132, 0.68351273, 0.98580414,
1.28809555, 1.59038695, 1.89267836, 2.19496977, 2.49726118,
2.79955259]),
<a list of 20 Patch objects>)
# kde画密度图
s.plot(kind='kde')
plt.show()