gpt4 book ai didi

python - 使用 matplotlib 将文本添加为​​星号

转载 作者:行者123 更新时间:2023-12-05 05:38:23 25 4
gpt4 key购买 nike

下面你可以在 matplotlib 中看到我的数据和刻面图。

import pandas as pd
import numpy as np
pd.set_option('max_columns', None)
import matplotlib.pyplot as plt

data = {
'type_sale': ['g_1','g_2','g_3','g_4','g_5','g_6','g_7','g_8','g_9','g_10'],
'open':[70,20,24,80,20,20,60,20,20,20],
'closed':[30,14,20,10,10,40,10,10,10,10],
}

df = pd.DataFrame(data, columns = ['type_sale',
'open',
'closed',
])

fig, axs = plt.subplots(2,2, figsize=(8,6))
plt.subplots_adjust(wspace=0.2, hspace=0.6)
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe',ax=axs[0,0])
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe', ax=axs[0,1])
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe',ax=axs[1,0])
df.plot(x='type_sale', kind='bar', stacked=True,title='Stacked Bar Graph by dataframe', ax=axs[1,1])

plt.suptitle(t='Stacked Bar Graph by dataframe', fontsize=16)
plt.show()

现在我想用黑线围起来,这个面,加星号的文字,类似下图

enter image description here

那么谁能帮我解决这个问题呢?

最佳答案

下面是我的实现方式,基于 this回答。

import pandas as pd
import numpy as np
pd.set_option('max_columns', None)
import matplotlib.pyplot as plt
import matplotlib as mpl

data = {
'type_sale': ['g_1','g_2','g_3','g_4','g_5','g_6','g_7','g_8','g_9','g_10'],
'open':[70,20,24,80,20,20,60,20,20,20],
'closed':[30,14,20,10,10,40,10,10,10,10],
}

df = pd.DataFrame(data, columns = ['type_sale',
'open',
'closed',
])

fig, axs = plt.subplots(2,2, figsize=(8,6))
plt.subplots_adjust(wspace=0.2, hspace=0.6)
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe',ax=axs[0,0])
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe', ax=axs[0,1])
df.plot(x='type_sale', kind='bar', stacked=True, title='Stacked Bar Graph by dataframe',ax=axs[1,0])
df.plot(x='type_sale', kind='bar', stacked=True,title='Stacked Bar Graph by dataframe', ax=axs[1,1])

plt.suptitle(t='Stacked Bar Graph by dataframe', fontsize=16)

### Changes
rect = mpl.patches.Rectangle((0.05,0.01), 0.9, 1, fill=False, color="k", lw=2,
zorder=1000, transform=fig.transFigure, figure=fig)
fig.patches.extend([rect])
fig.text(0.05, -0.02, '*) Source: Some institution')
fig.savefig('test.png', dpi=300, bbox_inches='tight')
####

plt.show()

输出:enter image description here

关于python - 使用 matplotlib 将文本添加为​​星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72923224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com