【发布时间】:2013-08-29 10:55:13
【问题描述】:
我正在尝试使用 Matplotlib 的 2d 散点图函数绘制一些数据,同时在 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'))。在他们的图片库中看到你想要的情节真是令人沮丧,但是这个例子不起作用。第二双眼睛将不胜感激!
【问题讨论】:
-
请将代码量减少到重现错误并超过 完整 堆栈跟踪所需的最少。
-
您使用的是什么版本的 matplotlib?我认为您遇到了错误。
-
下次我会尽量减少代码。大部分都是必要的,只是因为我希望人们看到我正在使用哪些界限和哪些数据。我正在使用 matplotlib 1.2.1 版。现在包括完整的堆栈跟踪。另外,我刚刚在我的另一台计算机上使用默认 matplotlibrc 文件和非 anaconda 打包发行版进行了尝试,所以这些似乎都不是原因。
-
这段代码在 matplotlib 1.3.0 中运行良好
标签: python matplotlib histogram scatter-plot