【问题标题】:Making more appealing the look of a bar plot with data of different magnitudes使用不同大小的数据使条形图的外观更具吸引力
【发布时间】:2021-07-23 23:56:47
【问题描述】:

我通过简单地调用 matplotlib 的 bar 方法生成了这个条形图,我的问题是,虽然大多数数据是相同数量级的,但有一些条形太长,无法压扁其余的酒吧以您无法比较的方式。结果如下:

我想绘制其余数据,就好像较高的条不存在一样,而较高的条只是为了到达图的顶部并在其上显示它们的真实值。

有可能吗?

为了完整起见,这是我生成情节的方式:

def plot_states_meanvar(l, ax):
    xs, means, var = zip(*l)
    xs = np.array(xs)
    for x in xs:
        ax.bar(x - 0.25/2, means[x], color = 'b', width = 0.25)
        ax.bar(x + 0.25/2, var[x], color = 'g', width = 0.25)
def plot_meanvar(meanvars, title=None):
    fig, axs = plt.subplots(ncols=3, nrows=2)
    for (modeldata, ax) in zip(meanvars, fig.axes):
        plot_states_meanvar(modeldata, ax)
    if title is not None:
        fig.suptitle(title)
    plt.tight_layout()

【问题讨论】:

  • ax.set_ylim()?还是改成对数刻度?
  • 如何更改为对数刻度?
  • @BigBen 谢谢,logscale 技巧奏效了,ylim 不太好,因为在设置了限制之后,情节被颠倒了。如果你写你的评论作为答案,我会接受它
  • @GerardoZinno ylim 把戏翻转了斧头,因为您只设置了最大值,但 ylim 在其参数中只看到一个值时认为它是最小值。改用plt.ylim((0, ymax))(参数是2元组)或plt.ylim(top=ymax)

标签: python matplotlib plot bar-chart


【解决方案1】:

一种选择是使用Axes.set_yscale 将 y 轴更改为对数刻度:

ax.set_yscale('log')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2019-05-24
    相关资源
    最近更新 更多