python数据分析-matplotlib核心包

本文详细探讨了Python中广泛使用的matplotlib库在数据分析中的应用。通过实例解析,介绍了如何利用matplotlib进行数据可视化,包括创建各种图表,如折线图、散点图、直方图等,以及自定义图表样式和颜色,提升数据展示效果。
摘要由CSDN通过智能技术生成
###########matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[2,4,6,1,3],"o",label='LineA')  ##折线图,o为点,o-为点加线
plt.plot([5,4,3,2,1],[2,4,6,1,3],color='red',marker='v',label='LineB') 
plt.title('who are you')
plt.xlabel('x')
plt.ylabel('y')
#plt.xlim(0,5)
#plt.ylim(0,5)
#plt.xticks([0,2,4])
#plt.xticks(np.arange(0,10,2),['a','b','c','d','e'])
plt.legend()
plt.show()


##pandas与matplotlib

df = pd.read_csv('/Users/terry/Downloads/'+
            '数据分析课件资料/FinanceAnalysis/601318.csv',
            index_col='date',parse_dates=['date'])[['open','close','high','low']] 
df.plot()
plt.show()


#作业绘制y=x,y=x*x,y=3x^3+5x^2+2x+1,使用不同颜色的线加以区别
x = np.linspace(-100,100,10000)
y1 = x
y2 = x ** 2
y3 = 3*x**3+5*x**2+x*2+1
plt.plot(x,y1,color='red',label='y=x')
plt.plot(x,y2,color='green',label='y=x*x')
plt.plot(x,y3,color='blue',label='y=3x^3+5x^2+2x+1')
plt.ylim(-1000,1000)
plt.legend()
plt.show()

#####画布与子图
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.plot([1,2,3,4],[5,6,7,8])
plt.show()

ax2 = fig.add_subplot(2,2,2)
fig.show()

fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

fig.show()

####柱形图和饼图

plt.bar([1,2,3,4],[5,6,7,8])
plt.show()

data= [32,45,34,65]
labels = ['Jan','Feb','Mar','Apr']

plt.bar(np.arange(len(data)),data,color='red',width=0.3)
plt.xticks(np.arange(len(data)),labels)
plt.show()


plt.pie([10,20,30,40],
        labels=['Jan','Feb','Mar','Apr'],
        autopct='%.0f%%',
        explode=[0,0,0.1,0])
plt.axis('equal')
plt.show()


########K线图
#matplotlib.finanace子包中有许多绘制金融相关图的函数接口
#matplotlib.finanace.candlestick_ochl
import mpl_finance as fin
from matplotlib.dates import date2num

df['time']=date2num(df.index.to_pydatetime())
df

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
arr = df[['time','open','close','high','low']].values

fin.candlestick_ochl(ax, arr)

plt.grid()
fig.show()
plt.show()


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值