簡體   English   中英

matplotlib scatter_hist 在直方圖中具有逐步填充的 histtype

[英]matplotlib scatter_hist with stepfilled histtype in histogram

我修改了此處找到的 scatter_hist.py 示例以繪制兩個數據集。

我想要具有“步進填充”類型的直方圖,但不知何故,如果我將類型設置為“步進填充”,則 Y 軸直方圖(方向 =“水平”)不起作用。

有沒有其他方法可以使直方圖看起來像“階梯式”風格,或者我做錯了什么?

這是我的帶有 histtype = "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()

謝謝你的幫助!

編輯:

這是我在 matplotlib 1.0.0 的 windows 環境中收到的圖像。 使用 histtype="bar" 我有這個:

條形直方圖圖像 和 histtype="stepfilled" 我有這個:

階梯式直方圖圖像

文檔只提到了使用“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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM