Python 将哈希模式应用于条形图

Python 将哈希模式应用于条形图,python,pandas,matplotlib,bar-chart,seaborn,Python,Pandas,Matplotlib,Bar Chart,Seaborn,我想创建一个列表,在那里我可以添加英语国家,并确保在我的条形图上反映出来 top9 = master_frame[master_frame['country_code'].isin(["USA","CHN","GBR","IND","CAN","ISR","FRA","DEU","SWE"])] plt.figure

我想创建一个列表,在那里我可以添加英语国家,并确保在我的条形图上反映出来

top9 = master_frame[master_frame['country_code'].isin(["USA","CHN","GBR","IND","CAN","ISR","FRA","DEU","SWE"])]

plt.figure(figsize=(10,5))
ax2=sns.barplot(x='country_code', y='raised_amount_usd', data=top9, estimator=np.sum,)
ax2.set_yscale('log')
ax2.set(xlabel='Funding by country', ylabel='Total amount of investments')
ax2.set_title('Investment distribution Country wise',fontsize =18, weight='bold')
plt.show()
输出:

这里的挑战是,我需要强调以英语为官方语言的国家,因此我的想法是在这些国家的栏中添加一个哈希模式,并在上面显示一个色调框

你知道我怎样才能做到这一点吗


提前谢谢你

我不太确定你提到的哈希模式

这是我的理解,你要做到两件事

首先,给具有不同属性的条上色

其次,在绘图上创建一个图例框

下面的方法是实现您的目标的方法


1.给条子上色。 通过在函数
sns.barplot
中设置参数
调色板
,可以轻松完成此步骤。您应以十六进制格式输入一系列颜色(也存在)

2.创建一个图例。 为了创建一个定制的图例框,我们可以设置手动创建的轴的手柄(我借用了代码的想法)


最终代码如下所示:

import matplotlib.patches as mpatches

top9 = master_frame[master_frame['country_code'].isin(["USA","CHN","GBR","IND","CAN","ISR","FRA","DEU","SWE"])]

plt.figure(figsize=(10,5))

color_eng = "#94698b"
color_non_eng = "#4369ef"

# the sequence length is the number of bars.
# Shall be composed by you
palette = [color_eng, color_eng, color_non_eng, color_eng, .......]

ax2 = sns.barplot(x='country_code', y='raised_amount_usd', 
                  data=top9, estimator=np.sum, palette=palette)

p_eng = mpatches.Patch(color=color_eng, label='English speaking country')
p_non_eng = mpatches.Patch(color=color_non_eng, label='non-English speaking country')

ax2.legend(handles=[p_eng, p_non_eng])

ax2.set_yscale('log')
ax2.set(xlabel='Funding by country', ylabel='Total amount of investments')
ax2.set_title('Investment distribution Country wise',fontsize =18, weight='bold')
plt.show()


我已尝试此操作,但出现以下错误:
ValueError:无效的RGBA参数:“4369ef”ValueError:无法为
import matplotlib.patches as mpatches

p_eng = mpatches.Patch(color=color_eng, label='English speaking country')
p_non_eng = mpatches.Patch(color=color_non_eng, label='non-English speaking country')

ax2.legend(handles=[p_eng, p_non_eng])
import matplotlib.patches as mpatches

top9 = master_frame[master_frame['country_code'].isin(["USA","CHN","GBR","IND","CAN","ISR","FRA","DEU","SWE"])]

plt.figure(figsize=(10,5))

color_eng = "#94698b"
color_non_eng = "#4369ef"

# the sequence length is the number of bars.
# Shall be composed by you
palette = [color_eng, color_eng, color_non_eng, color_eng, .......]

ax2 = sns.barplot(x='country_code', y='raised_amount_usd', 
                  data=top9, estimator=np.sum, palette=palette)

p_eng = mpatches.Patch(color=color_eng, label='English speaking country')
p_non_eng = mpatches.Patch(color=color_non_eng, label='non-English speaking country')

ax2.legend(handles=[p_eng, p_non_eng])

ax2.set_yscale('log')
ax2.set(xlabel='Funding by country', ylabel='Total amount of investments')
ax2.set_title('Investment distribution Country wise',fontsize =18, weight='bold')
plt.show()