【问题标题】:Is it possible to generate a plot shown in the enclosed figure using matplotlib?是否可以使用 matplotlib 生成图中所示的图?
【发布时间】:2018-08-23 03:17:46
【问题描述】:

我想使用 matplotlib 在子图网格上绘制一系列折线图。但是,网格的右上角部分是左下角的重复,所以我想省略右上角。我附上了我正在寻找的 png 图像。使用matplotlib可以画出这样的东西吗?

标记轴也很有用,但使用并不困难的子图。

对于那些感兴趣的人,这是具有四个变量的化学反应系统的一系列相图。我正在将每个变量与另一个变量进行对比。请注意,主对角线表示针对自身绘制的变量,也可能会被忽略。

我现在使用的代码可能看起来不太有用,但这是我以前做一个完整的 4 x 4 网格的方法,我想用右上角的左块做一个 4 x 4出去。请注意,如果我不绘制某些网格单元格,我仍然会显示坐标轴,也许我可以关闭这些子图的坐标轴绘制?

plt.subplots(4,4,figsize=(9,6))
slist = ['S1', 'S2', 'S3', 'S4']
count = 1
for i in range (4):
    for j in range (4):       
        r.reset()
        m = r.simulate (0, 80, 2000, [slist[i], slist[j]])
        plt.subplot (4,4,count)
        plt.plot (m[:,0], m[:,1])
        count += count
plt.show()

以下行调用一个模拟器,该模拟器返回当前所选变量的两列。它可能效率不高,因为我真的应该用所有变量调用一次,然后在每个情节中挑选出我想要的变量。但这只是一个尝试绘图的原型。可以稍后进行优化。变量 r 是对 simulstor 的引用。

m = r.simulate (0, 80, 2000, [slist[i], slist[j]])

我刚刚发现了这个:

  ax = plt.subplot (4,4,count)
  plt.plot (m[:,0], m[:,1])
  if count in [2,3,4,7,8,12]:
     ax.set_visible(False)

这很简单,但需要概括。

运行良好且基于 Kota Mori 的答案的最终代码,其中还包括轴标签:

slist = ['S1', 'S2', 'S3', 'S4']
m = r.simulate (0, 80, 2000, slist)
fig = plt.figure(figsize=(9,6))
fig.tight_layout() 
count = 0
for i in range(4):
    for j in range(4):
        count += 1
        if i >= j:
           ax = fig.add_subplot(4, 4, count)
           ax.set_xlabel (slist[i])
           ax.set_ylabel (slist[j])
           ax.plot (m[:,i], m[:,j])

标签: python matplotlib


【解决方案1】:

您可以使用add_subplot 仅添加您需要的单元格。

fig = plt.figure(figsize=(9, 6))

count = 0
for i in range(4):
    for j in range(4):
        count += 1
        if i >= j:
            ax = fig.add_subplot(4, 4, count)
            ax.text(0.5, 0.5, str(count))

【讨论】:

    【解决方案2】:

    你能试试这个吗?

    plt.subplots(4,4,figsize=(9,6))
    slist = ['S1', 'S2', 'S3', 'S4']
    for i in range (4):
        for j in range (4): 
            if i>=j:
              pos = i*4+j+1
              r.reset()
              m = r.simulate (0, 80, 2000, [slist[i], slist[j]])
              plt.subplot (4,4,pos)
              plt.plot (m[:,0], m[:,1])
    plt.show()
    

    【讨论】:

    • 这部分有效,但它绘制了缺失网格单元的轴。我遇到了这个: ax.set_visible(False) 这将隐藏轴。
    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 2013-07-19
    相关资源
    最近更新 更多