gpt4 book ai didi

python - Matplotlib 步进图旋转

转载 作者:行者123 更新时间:2023-12-04 12:28:24 24 4
gpt4 key购买 nike

我正在尝试正确旋转 matplotlib 步骤图。首先,我交换了 x 和 y 轴并反转了 y 轴。我再次制作了步骤图。然而,右图中的阶梯线(蓝色)的方向并不如预期,红色阶梯线是左图的叠加旋转图像。这是我的代码

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(14)
y = np.sin(x / 2)

fig, (ax, bx) = plt.subplots(nrows=1, ncols=2, figsize=(11.5, 5.5))
fig.subplots_adjust(left=0.08, bottom=0.13, top=0.98, right=0.97, wspace=0.2, hspace=0.0)

ax.step(x, y, where='mid', c='r')
ax.plot(x, y, 'o--', color='grey', alpha=0.3)

bx.invert_yaxis()
bx.step(y, x, where='pre', c='b')
bx.plot(y, x, 'o--', color='grey', alpha=0.3)

plt.show()
我正在尝试制作如右图所示的红色阶梯图。我怎样才能做到这一点?
enter image description here

最佳答案

可以通过稍微移动第二个坐标并使用 where=pre 来获得所需的步进样式。 .

def plot_step_invert_mid(x, y, *args, **kwargs):
y_new = np.insert(y, 0, y[0])
y_new = 0.5 * (y_new[1:] + y_new[:-1])
x_new = np.append(x, x[-1])
y_new = np.append(y_new, y[-1])
plt.step(x_new, y_new, where="pre", *args, **kwargs)

fig, (ax, bx) = plt.subplots(nrows=1, ncols=2, figsize=(11.5, 5.5))
fig.subplots_adjust(left=0.08, bottom=0.13, top=0.98, right=0.97, wspace=0.2, hspace=0.0)

ax.step(x, y, where="mid", c='r')
ax.plot(x, y, 'o--', color='grey', alpha=0.3)

bx.invert_yaxis()
plot_step_invert_mid(y, x)
bx.plot(y, x, 'o--', color='grey', alpha=0.3)
results

关于python - Matplotlib 步进图旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68682606/

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