【问题标题】:matplotlib scatter_hist with stepfilled histtype in histogrammatplotlib scatter_hist 在直方图中具有逐步填充的 histt​​ype
【发布时间】:2011-06-28 15:14:44
【问题描述】:

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

我想要具有“步进填充”类型的直方图,但不知何故,如果我将类型设置为“步进填充”,则 Y 轴直方图(方向 =“水平”)不起作用。

有没有其他方法可以使直方图看起来像“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" 我有这个:

和 histt​​ype="stepfilled" 我有这个:

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    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])
    

    【讨论】:

    • 感谢 Henning 的回答。您的 matplotlib 版本是什么,您是否在 Windows 上运行它?
    • 我在 windows 7 上的 python 2.6.6 中运行 matplotlib 1.0.0 版
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2016-04-25
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多