数据可视化matplotlib的应用

本文介绍了使用Matplotlib绘制五种常见图表的方法,包括折线图、柱状图、直方图、堆栈图和饼状图,并提供了详细的代码示例。

1.plot折线图:    

import matplotlib.pyplot as plt

x = [1,2,3]
y = [5,7,4]

x2 = [1,2,3]
y2 =[10,14,12]

plt.plot(x,y,label='first line')
plt.plot(x2,y2,label='second line')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()

plt.show()


2.bar柱状图

import matplotlib.pyplot as plt

x = [2,4,6,8,10]
y = [8,6,2,5,6]

x2 = [1,2,3,4,10]
y2 =[10,14,12,5,7]

x3 = [1,3,5,7,9]
y3 = [2,3,5,7,4]

plt.bar(x,y,label='first line',color='g')
plt.plot(x2,y2,label='second line')
plt.bar(x3,y3,label='third line',color='b')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()

plt.show()


3.hist显示所在区间占有比例

import matplotlib.pyplot as plt

population_ages = [22,55,62,45,21,22,34,42,4,99,102,110]
bins = [0,10,30,40,50,70,80,90,100,110]

plt.hist(population_ages,bins,color='r')            #显示在bins区间上population_ages所占的比例
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()

plt.show()


4.stockplot堆栈式的

import matplotlib.pyplot as plt

days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,2,3]
working = [7,8,9,8,7]
playing = [8,5,7,8,13]

plt.plot([],[],color='r',label='sleeping', linewidth=5)
plt.plot([],[],color='b',label='eating', linewidth=5)
plt.plot([],[],color='g',label='working', linewidth=5)
plt.plot([],[],color='m',label='playing', linewidth=5)

plt.stackplot(days, sleeping, eating, working, playing, colors=['r','b','g','m',])
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()

plt.show()


5.pie饼状图

import matplotlib.pyplot as plt

days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,2,3]
working = [7,8,9,8,7]
playing = [8,5,7,8,13]

slices = [7,2,2,13]
activities = ['sleeping', 'eating', 'working', 'playing']

plt.pie(slices, labels=activities, colors=['r','c','b','g'], startangle=90, shadow=True, explode=(0,0.1,0,0))

plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\nCheck it out')
plt.legend()

plt.show()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值