【问题标题】:Matplotlib- plotting a combined line/bar chart within a subplot- can't see both linesMatplotlib-在子图中绘制组合线/条形图-看不到两条线
【发布时间】:2017-12-22 03:05:44
【问题描述】:

我有一份报告,我正在尝试添加几个子图。使用这个例子,我能够创建一个组合的线/条图How to align the bar and line in matplotlib two y-axes chart?

但是,我现在需要将此作为子图添加到现有图(如果有意义的话)。就格式而言,一切都对我有用,但由于某种原因,我无法同时显示线条和条形,一次只能显示 1 个。这是我配置的代码,有人可以告诉我我做错了什么吗?

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8.5,11))
ax2 = plt.subplot2grid((4, 2), (0, 1))

def billions(x, pos):
    'The two args are the value and tick position'
    return '$%1.1fB' % (x*1e-9)

def millions(x, pos):
    'The two args are the value and tick position'
    return '$%1.1fM' % (x*1e-6)

formatterb = plt.FuncFormatter(billions)
formatterm = plt.FuncFormatter(millions)

barax = ax2.twinx()

data = growthtable[['date','total','profit']]

barax.set_ylabel('Total')
ax2.set_ylabel('Profit')

barax.xaxis.tick_top()
barax.yaxis.set_major_formatter(formatterm)
ax2.yaxis.set_major_formatter(formatterb)
barax.set_title('Revenue and Profits')

data['Revenue'].plot(kind='bar',ax=ax2,facecolor='blue')
data['Profit'].plot(ax=ax2)

看起来非常简单/标准,但由于某种原因,根据我放置最后两行的顺序,我要么看到利润要么看到收入,而不是两者。

更新代码我得到这个:

ax2 = plt.subplot2grid((4, 2), (0, 1))

def billions(x, pos):
    'The two args are the value and tick position'
    return '$%1.1fB' % (x*1e-9)

def millions(x, pos):
    'The two args are the value and tick position'
    return '$%1.1fM' % (x*1e-6)

formatterb = FuncFormatter(billions)
formatterm = FuncFormatter(millions)


barax = ax2.twinx()





barax.plot(data.index, data['Profit'], linewidth=3, color='#a64dff')


barax.xaxis.tick_top()
barax.yaxis.set_major_formatter(formatterm)
ax2.yaxis.set_major_formatter(formatterb)

ax2.bar(data.index, data['Revenue'], color='blue')

但是,正如您所见,我正在尝试更改条形图的大小和颜色,但这不起作用?

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    我会稍微不同地处理这个问题。通常我会提前定义我的子图,以便于参考。

    fig, (ax1, ax1) = plt.subplots(2, sharex=True)
    
    df.profit.plot(ax=ax1)
    df.revenue.plot(kind='bar', ax=ax2)
    

    您可以使用相同的方式进行格式化,只需确保引用正确的AxesSubplot

    编辑(根据@DickThompsons cmets)

    如果要将两个图叠加到一个图上,则需要使用 twin 返回的AxesSubplot 进行绘图。

    在您的示例中,您同时使用ax2

    data['Revenue'].plot(kind='bar',ax=ax2,facecolor='blue')
    data['Profit'].plot(ax=ax2)
    

    您应该将ax2 用于其中一个,将barax 用于另一个:

    data['Revenue'].plot(kind='bar',ax=barax, facecolor='blue')
    data['Profit'].plot(ax=ax2)
    

    所有其他条件相同,应创建您描述的图形并将其放置在图形的右上角。这是我创建的一个示例。

    fig = plt.figure()
    ax = plt.subplot2grid((4,2), (0,1))
    ax2 = ax.twinx()
    df.Profit.plot(ax=ax)
    df.Revenue.plot(kind='bar', ax=ax2)
    ax.yaxis.set_major_formatter(formatterm)
    ax2.yaxis.set_major_formatter(formatterb)
    fig.show()
    

    【讨论】:

    • 你明白我是如何定义ax2的吗? ax2 = plt.subplot2grid((4, 2), (0, 1)) 我试图将它们保留在右上角的子图中,那么在合并 gridspec 时我将如何做到这一点?如果有意义的话,我认为我不能使用它
    • 我不确定我是否遵循。您是否尝试将这两个子图放置在另一个子图中,索引为 0,1 的 4x2 网格?
    • 与此示例类似,stackoverflow.com/questions/19952290/…,我正在尝试制作组合条形图/折线图。我只想把它放在 4x2 网格上的子图索引 0,1 中——就像你说的那样。那么如何将第一行合并到我的代码中呢?这就是我困惑的地方
    • @DickThompson 我更新了答案以反映您的 cmets。你是这个意思吗?
    • 因此,对于您建议进行编辑的第一个示例,我已经拥有了-它给了我同样的问题,它显示了一个或另一个。我尝试将我的代码修改为您的示例,但仍然遇到同样的问题。出于某种原因,如果我正在绘制 2 条线,它会显示出来,但是如果我尝试一个条形图和一条线,则只有其中一条线显示
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多