gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:09:14 25 4
gpt4 key购买 nike

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

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

enter image description here

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

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

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

以下行调用一个模拟器,该模拟器返回当前选定变量的两列。它的效率并不高,因为我真的应该对所有变量调用一次,然后在每个图中挑选出我想要的变量。但这只是实验绘图的原型(prototype)。优化可以稍后进行。变量 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])

最佳答案

您可以使用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))

关于python - 是否可以使用 matplotlib 生成附图所示的图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51977872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com