是否可以使用matplotlib生成附图所示的图?

时间:2018-08-23 03:17:47

标签: python matplotlib

我想使用matplotlib在子图网格上绘制一系列折线图。但是,网格的右上部分是左下角的重复,因此我想省略右上角。我附上我要寻找的png图像。可以使用matplotlib绘制类似的东西吗?

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

enter image description here

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

我目前使用的代码可能对查看不是很有用,但这是我用来做一个完整的4×4网格的东西,我想在左上角的右上方做一个4×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是对仿真器的引用。

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

2 个答案:

答案 0 :(得分:3)

您可以使用!pip install pyhdf仅添加所需的单元格。

add_subplot

答案 1 :(得分:0)

可以请您试试吗?

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