#38#Pandas绘图之Seriesimport pandas as pdimport numpy as npfrom pandas import Series,DataFrameimport matplotlib.pyplot as plt """#cumsum()函数求和s = Series([1,2,3,4,5])print(s)# 0 1# 1 2# 2 3# 3 4# 4 5# dtype: int64print(s.cumsum())#递加求和# 0 1# 1 3# 2 6# 3 10# 4 15# dtype: int64 s1 = Series(np.random.randn(1000)).cumsum()s2 = Series(np.random.randn(1000)).cumsum()# s1.plot(kind="line",grid=True,label="S1",title="This is Series",style="--")# s2.plot(label="S2")# plt.legend()#将label图例显示出来# plt.show() #画子图:方法1# fig,ax = plt.subplots(2,1)#两行一列的子图,ax是画笔# ax[0].plot(s1)# ax[1].plot(s2)# plt.show() #画子图:方法2# fig,ax = plt.subplots(2,1)#两行一列的子图,ax是画笔# s1.plot(ax=ax[0],label="S1")# s2.plot(ax=ax[1],label="S2")# plt.show() #设置不同图像款式fig,ax = plt.subplots(2,1)#两行一列的子图,ax是画笔s1[:10].plot(ax=ax[0],label="S1",kind="bar")s2.plot(ax=ax[1],label="S2")plt.show()""" """#Pandas绘图之DataFramedf = DataFrame( np.random.randint(1,10,40).reshape(10,4), columns=["A","B","C","D"])#print(df)# A B C D# 0 9 5 4 9# 1 1 9 7 3# 2 1 1 2 6# 3 1 8 4 9# 4 1 8 8 4# 5 8 9 9 2# 6 1 7 1 6# 7 5 6 8 7# 8 8 6 4 6# 9 8 1 4 6#df.plot()#默认按照列画图#df.plot(kind="bar")#df.plot(kind="barh")#df.plot(kind="bar",stacked=True)#叠加条形图# df.plot(kind="area")#填充线形图# plt.show()## print(df.iloc[5])# # A 3# # B 8# # C 9# # D 2# # Name: 5, dtype: int32# df.iloc[5].plot()# plt.show() #按照行去画图:# for i in df.index:# df.iloc[i].plot(label=str(i))# plt.legend()# plt.show() #按列画图:# df["A"].plot()# plt.show() #按照行画图简单用法# df.T.plot()# plt.show()""" #------------#matplotlib的直方图和密度图#直方图:s = Series(np.random.randn(1000))# plt.hist(s)# plt.show() # re = plt.hist(s,rwidth=0.9)# print(type(re))#<class ‘tuple‘># print(len(re))#3# print(re[0])#[ 8. 44. 107. 178. 245. 198. 135. 61. 19. 5.]#代表频率,出现的次数# print(re[1])#[-3.41543154 -2.7680023 -2.12057305 -1.47314381 -0.82571456 -0.17828532 0.46914393 1.11657317 1.76400242 2.41143166 3.05886091]# #表示取值的间隔# print(re[2])#<a list of 10 Patch objects>#表示有10个矩形 #设置相关参数# plt.hist(s,rwidth=0.9,bins=20,color="r")# plt.show() #密度图画法:# s.plot()# plt.show()#kde画密度图:s.plot(kind=‘kde‘)plt.show()
原文地址:https://www.cnblogs.com/nikecode/p/11130971.html
时间: 2024-10-12 07:03:55