【问题标题】:Fixing axes ranges in pyplot subplots修复 pyplot 子图中的轴范围
【发布时间】:2012-12-18 02:20:47
【问题描述】:

我正在尝试使用 Python 的 matplotlib.pyplot 子图绘制数据集 U(x,t) 和 V(x,t)。我想手动设置第一个子图的轴,而第二个子图可以选择自己的轴:

import matplotlib.pyplot as plt

plt.subplot(121)
plt.pcolor(xx,tt,U)
plt.colorbar()
plt.axes([0,600,0,100])
plt.subplot(122)
plt.pcolor(xx,tt,V)
plt.colorbar()
plt.show()

但是,这似乎对更改第一个子图的轴没有影响。此外,情节生成后,我得到一个广泛的错误:

Exception in Tkinter callback
...
...
raise LinAlgError('Singular matrix')
numpy.linalg.linalg.LinAlgError: Singular matrix

但是,当我从代码中删除上述绘图命令时,错误消失了。有什么想法吗?

【问题讨论】:

  • 你能分享一些 xx、tt、U 和 V 的样本数据吗?所以我们可以尝试运行您的示例。
  • 当然!例如:from numpy import *xx = arange(0,700,30)tt = arange(0,100,5)U = outer(xx,tt)V = outer(xx**(1/2),tt/4)。由于某种原因,我不再收到错误消息,但情节仍然是错误的。谢谢!
  • 太好了,谢谢。使用该示例数据,我遇到了“形状不匹配”错误。请注意,在下面的答案中,我在 pcolor 函数调用中翻转了 xxtt 顺序来为我修复它。

标签: python-3.x matplotlib tkinter subplot


【解决方案1】:

您可以尝试调整 xlim 和 ylim 属性,而不是尝试设置轴本身:

import matplotlib.pyplot as plt
from numpy import *
xx = arange(0,700,30)
tt = arange(0,100,5)
U = outer(xx,tt)
V = outer(xx**(1/2),tt/4)
plt.subplot(121)
plt.pcolor(tt,xx,U)
plt.colorbar()
#plt.axes([0,600,0,100])

plt.xlim(0,100)
plt.ylim(0,600)

plt.subplot(122)
plt.pcolor(tt,xx,V)
plt.colorbar()

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 2021-10-27
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2015-05-23
    相关资源
    最近更新 更多