在matplotlib直方图中排除一定范围的垃圾箱?

时间:2018-08-01 15:22:50

标签: python pandas matplotlib histogram data-visualization

我正在使用matplotlib来查看基于MLB的赔率如何分配获胜。问题是,因为投注赔率是> = 100或<= -100,所以我的直方图中间存在很大差距。

有什么方法可以排除某些垃圾箱(特别是-100到100之间的任何垃圾箱),以使图表的条形更加流畅?

Link to current histogram

这是我现在拥有的代码:

num_bins = 20
fig, ax = plt.subplots()

n, bins, patches = ax.hist(winner_odds_df['WinnerOdds'], num_bins, 
range=range_of_winner_odds)

ax.set_xlabel('Betting Odds')
ax.set_ylabel('Win Frequency')
ax.set_title('Histogram of Favorite Win Frequency Based on Betting Odds (2018)')

fig.tight_layout()
plt.show()

1 个答案:

答案 0 :(得分:0)

您可以按照here的说明断开图表的x轴,方法是在两个不同的轴上绘图,使它们看起来像一个绘图。重写以应用于x轴而不是y轴的基本部分是:

f, (axl, axr) = plt.subplots(1, 2, sharey=True)

# plot the same data on both axes
axl.hist(winner_odds_df['WinnerOdds'], num_bins)
axr.hist(winner_odds_df['WinnerOdds'], num_bins)

# zoom-in / limit the view to different portions of the data
axl.set_xlim(-500, -100)  # outliers only
axr.set_xlim(100, 500)  # most of the data

# hide the spines between axl and axr
axl.spines['right'].set_visible(False)
axr.spines['left'].set_visible(False)

axr.yaxis.tick_right()
# How much space to leave between plots
plt.subplots_adjust(wspace=0.15)

请参阅链接的文档,以了解如何通过添加对角折线来进行抛光。上面的代码生成的基本版本如下所示: Normal distribution with broken x-axis