gpt4 book ai didi

python - 带有日期的 Twinx 会影响 future 绘图的 x 轴间隔

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

我正在使用 MatplotlibWidget 创建一个 GUI 应用程序。根据用户在 MatplotlibWidget 中的选择,图表可以是:

A.函数 y1(t),其中 y1 是浮点型,t 是日期时间,

B.函数 y1(t), y2(t),其中 y2 为 float ,y2 在 twinx 轴上绘制,或者

C.函数 y1(y2)(y1、y2 float ,而不是日期时间)

每次用户更改选择时,轴都会被清除,新的绘图将显示在 MatplotlibWidget 中。

问题是当绘制 B 选择然后绘制 C 时,x 轴保留日期间隔并添加新的 y2 间隔。这导致 x 轴间隔远离 y2 间隔。当我尝试仅绘制孪生 y2(t) 而主轴上没有 y1(t) 时,也会发生这种情况。

我还尝试在 twinx 中为 C 选择绘制一个空数据集,其中 xdata 与原始 xaxis 相同,它适用于此处介绍的简单代码,但在我的原始代码中不起作用。

一个想法是删除/移除 twinx 轴。我已经尝试过delaxes,但它仍然没有改变一些东西。

有什么想法为什么会发生这种情况以及如何解决这个问题吗?

谢谢!

这是代码的更简化版本:

from datetime import datetime
import matplotlib.pyplot as plt

y1 = range(0, 10)
y2 = range(-10, -20, -1)
t = [datetime(2015, 10, i) for i in range(1, 11)]

# Create the plot
plt.figure()
ax1 = plt.subplot(111)

# A. Plot y1(t)
ax1.plot(t, y1, 'b')
plt.xlabel('t')
plt.ylabel('y1')
plt.draw()
print 'Xaxis interval y1(t) ax1: ', ax1.get_xlim()
# plt.show()

# C. Plot y1(y2)
ax1.clear()
ax1.plot(y2, y1, 'r')
plt.gcf().autofmt_xdate()
plt.xlabel('y2')
plt.ylabel('y1')
plt.draw()
print 'Xaxis interval y1(y2) ax1:', ax1.get_xlim(), '(correct)'
# plt.show()

# B. Plot y1(t) and y2(t)
ax1.clear()
ax2 = ax1.twinx()
ax2.plot(t, y2, 'g')
ax1.plot(t, y1, 'b')
plt.xlabel('t')
plt.ylabel('y1')
ax2.set_label('y2')
plt.draw()
print 'Xaxis interval y2(t) ax1:', ax1.get_xlim()
print 'Xaxis interval y2(t) ax2: ', ax2.get_xlim()
# plt.show()


# # Plot only y2(t) in twinx
# ax1.clear()
# ax2 = ax1.twinx()
# ax2.plot(t, y2, 'g')
# plt.xlabel('t')
# ax2.set_label('y2')
# plt.draw()
# print 'Xaxis interval y2(t): ax1',ax1.get_xlim()
# print 'Xaxis interval y2(t): ax2',ax2.get_xlim()
# # plt.show()

# C. Plot y1(y2)
ax1.clear()
ax2.clear()
ax1.plot(y2, y1, 'r')
plt.xlabel('y2')
plt.ylabel('y1')
plt.draw()
print 'Xaxis interval y1(y2), ax1: ',ax1.get_xlim(), '(wrong)'
print 'Xaxis interval y1(y2) ax2: ',ax2.get_xlim(), '(wrong)'
plt.show()

结果:

Xaxis interval y1(t) ax1: (735872.0, 735881.0)

Xaxis interval y1(y2) ax1: (-19.0, -10.0) (correct)

Xaxis interval y2(t) ax1: (735872.0, 735881.0)

Xaxis interval y2(t) ax2: (735872.0, 735881.0)

Xaxis interval y1(y2), ax1: (-100000.0, 800000.0) (wrong)

Xaxis interval y1(y2) ax2: (-100000.0, 800000.0) (wrong)

最佳答案

我找到并适合我的一个解决方案是删除 ax1 和 ax2 并创建一个新的子图(新 ax1),当我想在创建 ax2 并在其上绘制日期时间后绘制 y1(y2) 时。

from datetime import datetime
import matplotlib.pyplot as plt

y1 = range(0, 10)
y2 = range(-10, -20, -1)
t = [datetime(2015, 10, i) for i in range(1, 11)]

# Create the plot
plt.figure()
ax1 = plt.subplot(111)

# B. Plot y1(t) and y2(t)
ax1.clear()
ax2 = ax1.twinx()
ax2.plot(t, y2, 'g')
ax1.plot(t, y1, 'b')
plt.xlabel('t')
plt.ylabel('y1')
ax2.set_label('y2')
plt.draw()
print 'Xaxis interval y2(t) ax1:', ax1.get_xlim()
print 'Xaxis interval y2(t) ax2: ', ax2.get_xlim()
# plt.show()

# C. Plot y1(y2)
plt.delaxes(ax1)
plt.delaxes(ax2)
ax1 = plt.subplot(111)
ax1.plot(y2, y1, 'r')
# ax2.plot(y2, [None for i in range(len(y2))])
plt.xlabel('y2')
plt.ylabel('y1')
plt.draw()
print 'Xaxis interval y1(y2), ax1: ',ax1.get_xlim()
plt.show()

X轴间隔y2(t) ax1:(735872.0, 735881.0)

X轴间隔y2(t) ax2:(735872.0, 735881.0)

X轴间隔y1(y2),ax1:(-19.0,-10.0)

关于python - 带有日期的 Twinx 会影响 future 绘图的 x 轴间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30910653/

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