【发布时间】: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