如何用python画组合图形 您所在的位置:网站首页 python组合图形绘制的收获 如何用python画组合图形

如何用python画组合图形

2023-07-16 17:56| 来源: 网络整理| 查看: 265

我是否可以在一个运行三次的for循环中制作一组子图(带有2个图),然后将三组子图组合成一个主图.这一点的重点是能够在一个图上有6个图,但在每个其他图之间有一个空格.我知道如何在一个图中有6个图,但我只能在每个图之间放置空格而不是每个其他图.我希望我的问题有道理.至于我正在使用的数据,它是我现在用于练习的非常基本的数据集.每对绘图共享相同的x轴,这就是为什么我不希望它们之间有空格.

import matplotlib.pyplot as plt

x1 = [0,1,2,3,4,5]

y1 = [i*2 for i in x1]

y2 = [i*3 for i in x1]

x2 = [4,8,12,16,20]

y3 = [i*5 for i in x2]

y4 = [i*3 for i in x2]

x3 = [0,1,2,3,4,5]

y5 = [i*4 for i in x3]

y6 = [i*7 for i in x3]

fig = plt.figure(1,figsize=(5,5))

ax1 = plt.subplot(611)

ax1.plot(x1,y1)

ax2 = plt.subplot(612)

ax2.plot(x1,y2)

ax3 = plt.subplot(613)

ax3.plot(x2,y3)

ax4 = plt.subplot(614)

ax4.plot(x2,y4)

ax5 = plt.subplot(615)

ax5.plot(x3,y5)

ax6 = plt.subplot(616)

ax6.plot(x3,y6)

fig.subplots_adjust(hspace=0.5)

plt.show()

这就是我得到的:

xYwUO.png

解决方法:

您的代码生成了包含六个子图的图表.如果您制作了8个子图并将其中两个留空,则会获得额外的空间.这是我使用的代码,稍微修改了你的代码.

import matplotlib.pyplot as plt

x1 = [0,1,2,3,4,5]

y1 = [i*2 for i in x1]

y2 = [i*3 for i in x1]

x2 = [4,8,12,16,20]

y3 = [i*5 for i in x2]

y4 = [i*3 for i in x2]

x3 = [0,1,2,3,4,5]

y5 = [i*4 for i in x3]

y6 = [i*7 for i in x3]

fig = plt.figure(1,figsize=(5,7))

ax1 = plt.subplot(811)

ax1.plot(x1,y1)

ax2 = plt.subplot(812)

ax2.plot(x1,y2)

ax3 = plt.subplot(814)

ax3.plot(x2,y3)

ax4 = plt.subplot(815)

ax4.plot(x2,y4)

ax5 = plt.subplot(817)

ax5.plot(x3,y5)

ax6 = plt.subplot(818)

ax6.plot(x3,y6)

fig.subplots_adjust(hspace=0.5)

plt.show()

我得到这个结果:

bkQew.png

我不得不将图形尺寸高度增加到7英寸以容纳额外的空间.那是你要的吗?

标签:python,matplotlib

来源: https://codeday.me/bug/20190627/1306885.html



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有