gpt4 book ai didi

python - matplotlib scatter_hist 在直方图中具有 stepfilled histt​​ype

转载 作者:太空狗 更新时间:2023-10-29 21:57:30 25 4
gpt4 key购买 nike

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

我想要“stepfilled”类型的直方图,但不知何故,如果我设置“stepfilled”类型,则 Y 轴直方图(方向 =“水平”)不起作用。

有没有其他方法可以使直方图看起来像“阶梯填充”样式,或者我做错了什么?

这是我的 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使用 histt​​ype="stepfilled"我有这个:

stepfilled histogram image

最佳答案

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

关于python - matplotlib scatter_hist 在直方图中具有 stepfilled histt​​ype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com