gpt4 book ai didi

python - 如何在子图中绘制图形 (Matplotlib)

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:38 24 4
gpt4 key购买 nike

我知道有多种方法可以在一个图中绘制多个图形。一种这样的方法是使用轴,例如

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([range(8)])
ax.plot(...)

因为我有一个函数可以美化我的图表并随后返回一个图形,我想使用该图形在我的子图中绘制。它看起来应该类似于:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(figure1) # where figure is a plt.figure object
ax.plot(figure2)

这不起作用,但我怎样才能让它起作用?有没有一种方法可以将图形放在子图中,或者有一种解决方法可以在一个整体图中绘制多个图形?

非常感谢对此的任何帮助。预先感谢您的意见。

最佳答案

如果目标只是自定义单个子图,为什么不更改函数以动态更改当前图形而不是返回图形。来自 matplotlibseaborn ,您可以在绘制时更改绘图设置吗?

import numpy as np
import matplotlib.pyplot as plt

plt.figure()

x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

import seaborn as sns

plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

plt.show()

enter image description here

也许我不完全理解你的问题。这个“美化”功能复杂吗?...

关于python - 如何在子图中绘制图形 (Matplotlib),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072367/

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