matplotlib scatter_hist在直方图中用stepfilled histt​​ype

时间:2011-06-28 15:14:45

标签: python matplotlib histogram

我修改了发现here的scatter_hist.py示例,以便绘制两个数据集。

我希望直方图有“stepfilled”类型,但不知何故,如果我设置“stepfilled”类型,Y轴直方图(orientation =“horizo​​ntal”)不起作用。

还有其他方法可以让直方图看起来像“stepfilled”式,还是我做错了什么?

这是我的代码与histt​​ype =“bar”,以显示我尝试做的想法。将其更改为

histtype="stepfilled"

得到奇怪的直方图:

import numpy as np
import matplotlib.pyplot as plt

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

x_vals = [x]
y_vals = [y]
x_vals.append( np.random.randn( 300 ) )
y_vals.append( np.random.randn( 300 ) )

fig = plt.figure(1, figsize=(5.5,5.5))

from mpl_toolkits.axes_grid1 import make_axes_locatable

colour_LUT = ['#0000FF',
              '#00FF00']

# the scatter plot:
xymax = np.max(np.fabs(x))
colors = []
axScatter = plt.subplot(111)
for i in range( len(x_vals ) ):
    colour = colour_LUT[i]
    xymax = np.max( [np.max(np.fabs(x)), np.max(np.fabs(y)), xymax ] )
    axScatter.scatter( x_vals[i], y_vals[i], color = colour )
    colors.append(colour)

axScatter.set_aspect(1.)

# create new axes on the right and on the top of the current axes
# The first argument of the new_vertical(new_horizontal) method is
# the height (width) of the axes to be created in inches.
divider = make_axes_locatable(axScatter)
axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)

# make some labels invisible
plt.setp(axHistx.get_xticklabels() + axHisty.get_yticklabels(),
         visible=False)

# now determine nice limits by hand:
binwidth = 0.25

lim = ( int(xymax/binwidth) + 1) * binwidth

bins = np.arange(-lim, lim + binwidth, binwidth)
histtype = "bar"
axHistx.hist(x_vals, bins=bins, histtype= histtype, color=colors)
axHisty.hist(y_vals, bins=bins, orientation='horizontal',histtype= histtype, color=colors)

# the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
# thus there is no need to manually adjust the xlim and ylim of these
# axis.

#axHistx.axis["bottom"].major_ticklabels.set_visible(False)
for tl in axHistx.get_xticklabels():
    tl.set_visible(False)
axHistx.set_yticks([0, 50, 100])

#axHisty.axis["left"].major_ticklabels.set_visible(False)
for tl in axHisty.get_yticklabels():
    tl.set_visible(False)
axHisty.set_xticks([0, 50, 100])

plt.draw()
plt.show()

谢谢你的帮助!

修改

以下是我在windows环境中使用matplotlib 1.0.0收到的图像。 随着histt​​ype =“bar”我有这个:

bar histogram image http://i54.tinypic.com/wunjoi.png 和histt​​ype =“stepfilled”我有这个:

stepfilled histogram image http://oi56.tinypic.com/rstu1j.jpg

1 个答案:

答案 0 :(得分:2)

documentation仅在使用'bar'和'barstacked'时提到多个数据的特殊情况,我认为这意味着其他两种类型没有正确实现。更改代码以添加多个直方图,而不仅仅是为我工作的直方图:

histtype = "stepfilled"
for i in xrange(len(x_vals)):
    axHistx.hist(x_vals[i], bins=bins, histtype= histtype, color=colors[i])
    axHisty.hist(y_vals[i], bins=bins, orientation='horizontal',histtype= histtype, color=colors[i])