python – 如何使用matplotlib在同一行中绘制多个图形?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何使用matplotlib在同一行中绘制多个图形?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2188字,纯文字阅读大概需要4分钟。
内容图文
![python – 如何使用matplotlib在同一行中绘制多个图形?](/upload/InfoBanner/zyjiaocheng/827/1e42723ccc1d4924a3084ef990a9d409.jpg)
在我的Ipython Notebook中,我有一个脚本会生成一系列多个数字,如下所示:
问题是,这些数字占用了太多空间,我正在制作许多这样的组合.这使我很难在这些数字之间导航.
我想在同一行中制作一些情节.我该怎么做?
更新:
感谢fjarri的建议,我已经更改了代码,这适用于绘制在同一行.
现在,我想让它们以不同的方式绘制(默认选项).我该怎么办?我尝试了一些,但不确定这是否正确.
def custom_plot1(ax = None):
if ax is None:
fig, ax = plt.subplots()
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
ax.plot(x1, y1, 'ko-')
ax.set_xlabel('time (s)')
ax.set_ylabel('Damped oscillation')
def custom_plot2(ax = None):
if ax is None:
fig, ax = plt.subplots()
x2 = np.linspace(0.0, 2.0)
y2 = np.cos(2 * np.pi * x2)
ax.plot(x2, y2, 'r.-')
ax.set_xlabel('time (s)')
ax.set_ylabel('Undamped')
# 1. Plot in same line, this would work
fig = plt.figure(figsize = (15,8))
ax1 = fig.add_subplot(1,2,1, projection = '3d')
custom_plot1(ax1)
ax2 = fig.add_subplot(1,2,2)
custom_plot2(ax2)
# 2. Plot in different line, default option
custom_plot1()
custom_plot2()
解决方法:
只需使用子图.
plt.plot(data1)
plt.show()
plt.subplot(1,2,1)
plt.plot(data2)
plt.subplot(1,2,2)
plt.plot(data3)
plt.show()
(这段代码不起作用,重要的是它背后的想法)
对于2号,同样的事情:使用子图:
# 1. Plot in same line, this would work
fig = plt.figure(figsize = (15,8))
ax1 = fig.add_subplot(1,2,1, projection = '3d')
custom_plot1(ax1)
ax2 = fig.add_subplot(1,2,2)
custom_plot2(ax2)
# 2. Plot in same line, on two rows
fig = plt.figure(figsize = (8,15)) # Changed the size of the figure, just aesthetic
ax1 = fig.add_subplot(2,1,1, projection = '3d') # Change the subplot arguments
custom_plot1(ax1)
ax2 = fig.add_subplot(2,1,2) # Change the subplot arguments
custom_plot2(ax2)
这不会显示两个不同的数字(这是我从“不同的线条”中理解的),而是将两个数字,一个在另一个之上,放在一个数字中.
现在,解释subplot参数:subplot(rows,cols,axnum)
行将是图形划分的行数.
cols将是该图被分成的列数.
axnum将是你要绘制的分区.
在你的情况下,如果你想并排两个图形,那么你想要一行有两列 – >副区(1,2,…)
在第二种情况下,如果你想要一个在另一个之上的两个图形,那么你想要2行和1列 – >副区(2,1,…)
对于更复杂的分发,请使用gridspec http://matplotlib.org/users/gridspec.html
内容总结
以上是互联网集市为您收集整理的python – 如何使用matplotlib在同一行中绘制多个图形?全部内容,希望文章能够帮你解决python – 如何使用matplotlib在同一行中绘制多个图形?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。