matplotlib可视化学习笔记

注:文章内容主要来自崔斯特知乎 https://www.zhihu.com/people/cuishite/posts/posts_by_votes

1.饼图画法

import matplotlib.pyplot as plt

labels = 'Frogs','Hogs','Dogs','Logs' #自定义标签
sizes = [15,30,45,10]   #每个标签占多大
explode = (0,0.1,0,0)  #将某部分爆炸出来

plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90)
    #autopct,圆里面的文本格式,%1.1f%%表示小数有1位,整数有一位的浮点数
    #shadow,饼是否有阴影
    #startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
plt.axis('equal')   # 设置x,y轴刻度一致,这样饼图才能是圆的
plt.show()

2.直方图

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)   #每次生成的随即数都相同
mu , sigma = 100,20    #均值和标准差
a = np.random.normal(mu,sigma,size=100)    #给出均值为mean,标准差为stdev的高斯随机数(场),当size赋值时,例如:size=100,表示返回100个高斯随机数。

plt.hist(a,10,histtype='stepfilled',facecolor='b',alpha=0.75)   #10是直方图的个数
plt.title('Histogram')  #标题
plt.show()

3.坐标图

import numpy as np
import matplotlib.pyplot as plt

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()

4.散点图

import numpy as np
import matplotlib.pyplot as plt

fig , ax = plt.subplots()
ax.plot(10*np.random.rand(100),10*np.random.rand(100),'o')
ax.set_title('Simple Scatter')

plt.show()

作者:崔斯特
链接:https://zhuanlan.zhihu.com/p/27401041
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  

posted @ 2017-09-26 15:36  骑公路喝茅台  阅读(291)  评论(0)    收藏  举报