Python数据分析与展示

(六)pyplot基础图表函数(学习笔记)|python数据分析

2018-01-29  本文已影响49人  努力奋斗的durian

1.pyplot基础图表函数概述
2.pyplot图饼的绘制
3.pyplot直方图的绘制
4.pyplot极坐标图的绘制
5.pyplot散点图的绘制
6.单元小结
[网页链接【Python数据分析与展示】.MOOC. 北京理工大学
https://www.bilibili.com/video/av10101509/?from=search&seid=8584212945516406240#page=27)

最近更新:2018-01-29

1.pyplot基础图表函数概述

重点是选什么样的图形与数据相对应




2.pyplot图饼的绘制

2.1扁形饼图

import matplotlib.pyplot as plt
labels="Frogs","Hogs","Dogs","Logs"
sizes=[35,30,45,10]
explode=(0,0.1,0,0)
plt.pie(sizes,explode=explode,labels=labels,autopct="%1.1f%%",shadow=False,startangle=99)
plt.show()

2.2圆饼图

扁形之间的区别是,增加了一行代码,plt.axis("equal")

import matplotlib.pyplot as plt
labels="Frogs","Hogs","Dogs","Logs"
sizes=[35,30,45,10]
explode=(0,0.1,0,0)
plt.pie(sizes,explode=explode,labels=labels,autopct="%1.1f%%",shadow=False,startangle=99)
plt.axis("equal")
plt.show()

3.pyplot直方图的绘制

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
mu,sigma=100,20
a=np.random.normal(mu,sigma,size=100)
plt.hist(a,20,normed=1,histtype="stepfilled",facecolor="b",alpha=0.75)
plt.title("Histogram")
plt.show()

将hist代码中的hist由原来的20分别改为10,40



hist改为10的代码及图像

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
mu,sigma=100,20
a=np.random.normal(mu,sigma,size=100)
plt.hist(a,10,normed=1,histtype="stepfilled",facecolor="b",alpha=0.75)
plt.title("Histogram")
plt.show()

hist改为40的代码及图像

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
mu,sigma=100,20
a=np.random.normal(mu,sigma,size=100)
plt.hist(a,40,normed=1,histtype="stepfilled",facecolor="b",alpha=0.75)
plt.title("Histogram")
plt.show()

理解直方图最关键的地方就是理解直方图的个数.

4.pyplot极坐标图的绘制

import matplotlib.pyplot as plt
import numpy as np

N=20
theta=np.linspace(0.0,2*np.pi,N,endpoint=False)
radii=10*np.random.rand(N)
width=np.pi/4*np.random.rand(N)

ax=plt.subplot(111,projection="polar")
bars=ax.bar(theta,radii,width=width,bottom=0.0)

for r,bar in zip(radii,bars):
    bar.set_facecolor(plt.cm.viridis(r/10.))
    bar.set_alpha(0.5)
    
plt.show()


最关键的代码行ax
修改参数,将n由原来的20改为10,pi/4改为pi/2,


import matplotlib.pyplot as plt
import numpy as np

N=10
theta=np.linspace(0.0,2*np.pi,N,endpoint=False)
radii=10*np.random.rand(N)
width=np.pi/2*np.random.rand(N)

ax=plt.subplot(111,projection="polar")
bars=ax.bar(theta,radii,width=width,bottom=0.0)

for r,bar in zip(radii,bars):
    bar.set_facecolor(plt.cm.viridis(r/10.))
    bar.set_alpha(0.5)
    
plt.show()

5.pyplot散点图的绘制

import matplotlib.pyplot as plt
import numpy as np

fig,ax=plt.subplots()
ax.plot(10*np.random.randn(100),10*np.random.randn(100),"o")
ax.set_title("Simple Scatter")
    
plt.show()

6.单元小结

上一篇下一篇

猜你喜欢

热点阅读