Matplotlib散点图和直方图

4
我正在尝试使用Matplotlib的二维散点图函数绘制一些数据,同时在x轴和y轴上生成投影直方图。我找到了一个例子,直接来自matplotlib图库(pylab_examples example code: scatter_hist.py)。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter

# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)

nullfmt   = NullFormatter()         # no labels

# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left+width+0.02

rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]

# start with a rectangular Figure
plt.figure(1, figsize=(8,8))

axScatter = plt.axes(rect_scatter)
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)

# no labels
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)

# the scatter plot:
axScatter.scatter(x, y)

# now determine nice limits by hand:
binwidth = 0.25
xymax = np.max( [np.max(np.fabs(x)), np.max(np.fabs(y))] )
lim = ( int(xymax/binwidth) + 1) * binwidth

axScatter.set_xlim( (-lim, lim) )
axScatter.set_ylim( (-lim, lim) )

bins = np.arange(-lim, lim + binwidth, binwidth)

axHistx.hist(x, bins=bins)
axHisty.hist(y, bins=bins, orientation='horizontal')

axHistx.set_xlim( axScatter.get_xlim() )
axHisty.set_ylim( axScatter.get_ylim() )

plt.show()

唯一的问题是示例无法工作。我收到以下错误:
~$ python ~/Desktop/scatter_and_hist.py 
Traceback (most recent call last):
  File "/Users/username/Desktop/scatter_and_hist.py", line 45, in <module>
    axHisty.hist(y, bins=bins, orientation='horizontal')
  File "//anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 8180, in hist
    color=c, bottom=bottom)
TypeError: barh() got multiple values for keyword argument 'bottom'

我已经查看了代码并找出了问题所在。问题出在第45行(axHisty.hist(y, bins=bins, orientation='horizontal'))。看到你想要的图表在他们的图像库中,但示例不起作用,真是令人沮丧。非常感谢您的帮助!

1
请将代码量减少到最小,以重现错误,并粘贴完整的堆栈跟踪。 - tacaswell
1
你使用的matplotlib版本是什么?我认为你遇到了一个bug。 - tacaswell
下次我会尽量少放一些代码。大部分都是必要的,只是因为我想让人们看到我使用的边界和数据。我正在使用matplotlib版本1.2.1。完整的堆栈跟踪现在已经包含在内。此外,我刚刚在我的另一台计算机上尝试了这个问题,它使用了默认的matplotlibrc文件和非anaconda打包的发行版,所以这两个都不是原因。 - astromax
1
这段代码在matplotlib 1.3.0中运行正常。 - Greg
你遇到了这个 https://github.com/matplotlib/matplotlib/pull/1985 的 bug。 - tacaswell
1个回答

5
您遇到了v1.2.1版(https://github.com/matplotlib/matplotlib/pull/1985)中的一个错误。您可以升级matplotlib、使用bug-fix来猴子补丁修复版本,或者使用np.histogram并自行调用具有正确参数顺序的barh。此外,为了回答这个问题,唯一需要的代码是:
x = np.random.rand(100)
plt.hist(x, orientation='horizontal')
plt.show()

您发布的其他内容都是噪音。


1
没错,那只是因为你知道 orientation='horizontal' 是罪魁祸首。如果我知道这是原因,我就只会发布相关的代码片段。然而,导致问题的可能性更高的是我作为用户的原因,而不是实际上是一个错误 - 因此我试图全面地提问。谢谢你的指点。我会尝试你的建议。 - astromax
1
@astromax 抱歉,最近几天我压力很大,情绪很糟糕。 - tacaswell
1
别担心 - 我没有把它当成个人问题。我尽量提供更多的信息,而不是少一点,但我同意,必须翻阅大量不必要的代码行可能会像没有足够的工作材料一样糟糕。 - astromax
你能详细说明一下“monkey patch your version with the bug-fix”是什么意思吗? - simona
1
@simona,通过“猴子补丁”,我指的是将该PR中的hist版本复制到您的代码中,并执行matplotlib.axes.Axes.hist = patched_hist或仅使用patch_hist(ax, ...)。 我强烈建议进行更新。 - tacaswell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接