matplotlib绘图实例:pyplot、pylab模块及作图参数Matplotlib.pyplot绘图实例
页⾯中有上百幅缩略图,打开之后都有源程序。
[]
{使⽤pyplot模块}
绘制直线、条形/矩形区域
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(-1, 2, .01)
s = np.sin(2 * np.pi * t)
plt.plot(t,s)
# draw a thick red hline at y=0 that spans the xrange
l = plt.axhline(linewidth=4, color='r')
plt.axis([-1, 2, -1, 2])
plt.show()
plt.close()
# draw a default hline at y=1 that spans the xrange
plt.plot(t,s)
l = plt.axhline(y=1, color='b')
plt.axis([-1, 2, -1, 2])
plt.show()
plt.close()
# draw a thick blue vline at x=0 that spans the upper quadrant of the yrange
plt.plot(t,s)
l = plt.axvline(x=0, ymin=0, linewidth=4, color='b')
plt.axis([-1, 2, -1, 2])
plt.show()
plt.close()
# draw a default hline at y=.5 that spans the the middle half of the axes
plt.plot(t,s)
l = plt.axhline(y=.5, xmin=0.25, xmax=0.75)
plt.axis([-1, 2, -1, 2])
plt.show()
plt.close()
plt.plot(t,s)
p = plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)
p = plt.axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
plt.axis([-1, 2, -1, 2])
plt.show()
效果图展⽰
Note: 设置直线对应位置的值显⽰:(max_x, 0, str(round(max_x, 2))),也就是直接在指定坐标写⽂字,不知道有没有其它⽅法?
[]
另⼀种绘制直线的⽅式
plt.hlines(hline, a().get_xlim()[0], a().get_xlim()[1], linestyles=line_style, colors=color)
在matplotlib中的⼀个坐标轴上画⼀条直线光标
matplotlib.widgets.Cursor
# set useblit = True on gtkagg for enhanced performance
# horizOn=True时,两个坐标都有显⽰光标
[]
⽰例
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2 * np.pi * t)
plt.plot(t, s1)
cursor = a(), horizOn=True, color='r', lw=1)
plt.show()
Note: ⼀个神奇的事情就是,Cursor()必须有⼀个赋值给cursor,否则并不会显⽰光标(ubuntu16.04下)。之前在windows下绘制时不⽤赋值也是会有光标显⽰的。
结果⽰图(随着光标的移动,在图中x坐标上会画⼀条竖线,并在下⽅显⽰坐标):
同时在两个⼦图的两个坐标轴之间画⼀条直线光标
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.sin(4*np.pi*t)
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(t, s1)
ax2 = fig.add_subplot(212, sharex=ax1)
ax2.plot(t, s2)
multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1)
plt.show()
[]
直⽅图hist
plt.hist(songs_plays, bins=50,range=(0, 50000), color='lightblue',density=True) Note: density是将y坐标按⽐例绘图,⽽不是数⽬。
hist转换成plot折线图
plt.hist直接绘制数据是hist图
plt.hist(z, bins=500, normed=True)
hist图转换成折线图
cnts, bins = np.histogram(z, bins=500, normed=True)
bins = (bins[:-1] + bins[1:]) / 2
plt.plot(bins, cnts)
[:histogram]
柱状图bar
⽤每根柱⼦的长度表⽰值的⼤⼩,它们通常⽤来⽐较两组或多组值。
bar()的第⼀个参数为每根柱⼦左边缘的横坐标;第⼆个参数为每根柱⼦的⾼度;第三个参数指定所有柱⼦的宽度,当第三个参数为序列时,可以为每根柱⼦指定宽度。
bar()不⾃动修改颜⾊。
⽰例1:
import numpy as np
import matplotlib.pyplot as plt
label_list = [100, 101, 200, 201, 900, 901]
cnt_list = [2520, 7491, 35411, 2407, 54556, 6254]
plt.bar(range(len(cnt_list)), cnt_list, label='label cnt', color='steelblue', alpha=1.0, width=0.8)
plt.legend()
plt.show()
⽰例2:
n = np.array([0,1,2,3,4,5])
x = np.linspace(-0.75, 1., 100)
fig, axes = plt.subplots(1, 4, figsize=(12,3))
axes[0].scatter(x, x + 0.25*np.random.randn(len(x)))
linspace numpyaxes[1].step(n, n**2, lw=2)
axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)
axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5);
Note: axes⼦图设置title: axes.set_title("bar plot")
分组条形图
import matplotlib.pyplot as plt
import numpy as np
# 构建数据
Y2016 = [15600,12700,11300,4270,3620]
Y2017 = [17400,14800,12000,5200,4020]
labels = ['北京','上海','⾹港','深圳','⼴州']
bar_width = 0.45
# 中⽂乱码的处理
# 绘图
plt.bar(np.arange(5), Y2016, label = '2016', color = 'steelblue', alpha = 0.8, width = bar_width)
plt.bar(np.arange(5)+bar_width, Y2017, label = '2017', color = 'indianred', alpha = 0.8, width = bar_width)
plt.xlabel('Top5城市')
plt.ylabel('家庭数量')
plt.title('亿万财富家庭数Top5城市分布')
plt.ylim([2500, 19000])
# 为每个条形图添加数值标签
for x2016,y2016 in enumerate(Y2016):
<(x2016, y2016+100, '%s' %y2016)
for x2017,y2017 in enumerate(Y2017):
<(x2017+bar_width, y2017+100, '%s' %y2017)
plt.legend()
plt.show()
垂直堆叠条形图

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。