简体   繁体   中英

Barplot in seaborn with several columns

I have the following code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("https://raw.githubusercontent.com/norhther/datasets/main/ccaa_vacunas.csv", decimal = ",", thousands = ".")

df2 = df.dropna()

df2.columns = df2.columns.str.replace(' ', '_')
df2.columns = df2.columns.str.replace("-", "_")
    
mask = (df2["CCAA"] != "España") & (df2["CCAA"] != "Fuerzas Armadas")

sns.barplot(y = "CCAA", x = "Porcentaje_de_dosis_administradas_por_100_habitantes", data = df2[mask])  

I would like to plot in one axis the variable CCAA and on the other, several bars for each CCAA : Dosis_entregadas_Pfizer , Dosis_entregadas_Moderna , Dosis_entregadas_AstraZeneca , Dosis_entregadas_Janssen . However, I don't know how to approach this.

You could use melt to convert the desired columns to "long form" . And then use hue= on the newly created column.

Here is an example:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.read_csv("https://raw.githubusercontent.com/norhther/datasets/main/ccaa_vacunas.csv",
                 decimal=",", thousands=".")

mask = (df["CCAA"] != "España") & (df["CCAA"] != "Fuerzas Armadas")
df = df[mask].dropna()
df_long = df.melt(id_vars=["CCAA"],
                  value_vars=["Dosis entregadas Pfizer", "Dosis entregadas Moderna", "Dosis entregadas AstraZeneca",
                              "Dosis entregadas Janssen"],
                  value_name="Dosis", var_name="Fabricante")

fig, ax = plt.subplots(figsize=(12, 10))
sns.barplot(y="CCAA", x="Dosis",
            estimator=np.sum, ci=None,
            hue="Fabricante", palette='Set2', data=df_long, ax=ax)
ax.ticklabel_format(axis='x', useOffset=False, style='plain')
sns.despine()
plt.tight_layout()
plt.show()

sns.barplot 融化的数据框

PS: df_long looks like:

            CCAA                Fabricante      Dosis
0      Andalucía   Dosis entregadas Pfizer  1653300.0
1         Aragón   Dosis entregadas Pfizer   332595.0
2       Asturias   Dosis entregadas Pfizer   319145.0
3       Baleares   Dosis entregadas Pfizer   184050.0
4       Canarias   Dosis entregadas Pfizer   362670.0
          ...                       ...        ...
9267      Murcia  Dosis entregadas Janssen    68300.0
9268     Navarra  Dosis entregadas Janssen    23750.0
9269  País Vasco  Dosis entregadas Janssen   101300.0
9270       Ceuta  Dosis entregadas Janssen     4150.0
9271     Melilla  Dosis entregadas Janssen     5750.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM