码迷,mamicode.com
首页 > 编程语言 > 详细

python数据分析-09pandas绘图

时间:2019-07-04 11:23:19      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:object   otl   频率   不同   atp   数据分析   frame   设置   lib   

#38
#Pandas绘图之Series
import pandas as pd
import numpy as np
from pandas import Series,DataFrame
import 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: int64
print(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绘图之DataFrame
df = 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()

python数据分析-09pandas绘图

标签:object   otl   频率   不同   atp   数据分析   frame   设置   lib   

原文地址:https://www.cnblogs.com/nikecode/p/11130971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!