下面,您可以在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()

Output: enter image description here

Python相关问答推荐

try 从网站获取表(ValueRight:如果使用所有纯量值,则必须传递索引)

如何推迟对没有公钥的视图/表的反射?

Python(Polars):使用之前的变量确定当前解决方案的Vector化操作

使用Python C API重新启动Python解释器

在Python中管理多个OpenGVBO和VAO实例

如何使用Selenium访问svg对象内部的元素

如何终止带有队列的Python进程?+ 队列大小的错误?

优化在numpy数组中非零值周围创建缓冲区的函数的性能

列表上值总和最多为K(以O(log n))的最大元素数

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

替换字符串中的多个重叠子字符串

如何让剧作家等待Python中出现特定cookie(然后返回它)?

如何标记Spacy中不包含特定符号的单词?

Mistral模型为不同的输入文本生成相同的嵌入

用Python解密Java加密文件

如何在Python数据框架中加速序列的符号化

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

部分视图的DataFrame

如何根据一列的值有条件地 Select 前N个组,然后按两列分组?

如何在Python中获取`Genericums`超级类型?