【问题标题】:Excluding a certain range of bins in a matplotlib histogram?在 matplotlib 直方图中排除一定范围的 bin?
【发布时间】:2019-01-09 05:54:11
【问题描述】:

我正在使用 matplotlib 来查看如何根据 MLB 的投注赔率分配胜利。问题是因为投注赔率要么 >= 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()

【问题讨论】:

  • 尝试使用bins = 'auto'。也许它可以提供帮助。

标签: python pandas matplotlib histogram data-visualization


【解决方案1】:

您可以按照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)

请参阅链接文档,了解如何通过添加对角折线来完善它。上面代码生成的基本版本如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2011-10-22
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 2013-04-03
    • 2016-10-14
    相关资源
    最近更新 更多