【问题标题】:Replace chart in facet with Matplotlib用 Matplotlib 替换构面中的图表
【发布时间】:2022-07-31 18:17:09
【问题描述】:

我正在使用 Matplotlib。我正在尝试用四个图表来制作一个方面。下面你可以看到我的代码:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap

# Poligon for plot
fig, axs = plt.subplots(2,2, figsize=(17,10))
#plt.subplots_adjust(wspace=0.2, hspace=0.6)
plt.subplots_adjust(wspace=0.2, hspace=0.3)


df.plot(x='year', kind='bar', stacked=True,title='Title 1',ax=axs[0,0],legend=True)
df.plot(x='year', kind='bar', stacked=True,title='Title 2',ax=axs[0,1],legend=None)  
df.plot(x='year', kind='bar', stacked=True,title='Title 3',ax=axs[1,0],legend=True)
df.plot(x='year', kind='bar', stacked=True,title='Title 4',ax=axs[1,1],legend=None) #<-This chart is need to be replaced

plt.suptitle(t='Description of...', fontsize=16)

现在我想用其他组合折线图更改最后一个数字。代码如下所示

fig, ax_1 = plt.subplots(figsize = (8, 5))
ax_2 = ax_1.twinx()
cmap = get_cmap('tab10')

ax_1.bar(df['year'], df['open'], label = 'open', color = cmap(0))
ax_2.plot(df['year'], df['closed'], label = 'closed', color = cmap(0.1),linewidth = '4.5')

handles_1, labels_1 = ax_1.get_legend_handles_labels()
handles_2, labels_2 = ax_2.get_legend_handles_labels()

ax_1.legend(handles = handles_1 + handles_2, labels = labels_1 + labels_2, loc = 'upper left', shadow = True)
ax_1.grid(axis = 'y')
ax_1.set_title('Sales comparison')
plt.show()

现在我想用下面代码中的组合折线图替换构面中的最后一个条形图。我试图将axs[1,1] 放在上面的代码中,但不起作用。那么有人可以帮我解决这个问题并更换图表吗?

【问题讨论】:

    标签: python pandas matplotlib plot data-visualization


    【解决方案1】:

    您可以将axs[1, 1]复制到ax_1中,然后用ax_2 = ax_1.twinx()创建一个辅助轴:

    ax_1 = axs[1, 1]
    ax_2 = ax_1.twinx()
    cmap = get_cmap('tab10')
    
    ax_1.bar(df['year'], df['open'], label = 'open', color = cmap(0))
    ax_2.plot(df['year'], df['closed'], label = 'closed', color = cmap(0.1),linewidth = '4.5')
    
    handles_1, labels_1 = ax_1.get_legend_handles_labels()
    handles_2, labels_2 = ax_2.get_legend_handles_labels()
    
    ax_1.legend(handles = handles_1 + handles_2, labels = labels_1 + labels_2, loc = 'upper left', shadow = True)
    ax_1.grid(axis = 'y')
    ax_1.set_title('Sales comparison')
    

    完整代码

    import pandas as pd
    import matplotlib.pyplot as plt
    from matplotlib.cm import get_cmap
    
    
    data = {'year': [2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023],
            'open': [70, 20, 24, 150, 80, 90, 60, 90, 20, 20, 20, 24, 150, 80, 90, 60, 90, 20, 30],
            'closed':[30, 14, 20, 10, 20, 40, 10, 10, 10, 10, 30, 14, 20, 10, 20, 40, 10, 10, 10]}
    
    df = pd.DataFrame(data, columns = ['year',
                                       'open',
                                       'closed'])
    
    # Poligon for plot
    fig, axs = plt.subplots(2,2, figsize=(17,10))
    #plt.subplots_adjust(wspace=0.2, hspace=0.6)
    plt.subplots_adjust(wspace=0.2, hspace=0.3)
    
    
    df.plot(x='year', kind='bar', stacked=True,title='Title 1',ax=axs[0,0],legend=True)
    df.plot(x='year', kind='bar', stacked=True,title='Title 2',ax=axs[0,1],legend=None)
    df.plot(x='year', kind='bar', stacked=True,title='Title 3',ax=axs[1,0],legend=True)
    # df.plot(x='year', kind='bar', stacked=True,title='Title 4',ax=axs[1,1],legend=None) #<-This chart is need to be replaced
    
    
    ax_1 = axs[1, 1]
    ax_2 = ax_1.twinx()
    cmap = get_cmap('tab10')
    
    ax_1.bar(df['year'], df['open'], label = 'open', color = cmap(0))
    ax_2.plot(df['year'], df['closed'], label = 'closed', color = cmap(0.1),linewidth = '4.5')
    
    handles_1, labels_1 = ax_1.get_legend_handles_labels()
    handles_2, labels_2 = ax_2.get_legend_handles_labels()
    
    ax_1.legend(handles = handles_1 + handles_2, labels = labels_1 + labels_2, loc = 'upper left', shadow = True)
    ax_1.grid(axis = 'y')
    ax_1.set_title('Sales comparison')
    
    
    plt.suptitle(t='Description of...', fontsize=16)
    
    plt.show()
    

    情节

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多