0

I know how to add text on plots, but I have no idea how to add text on a histogram subplot, also wondering to change the width between each column(name). Hope someone can help me!! This is an extension question of How to Plot a plot with multiple values?. I change ax.scatter to ax.bar, and added yerror. Here is my code current code with a toy dataframe.

Toy dataframe

import pandas as pd
import numpy as np

random_data = np.random.randint(10,25,size=(5,3))
df = pd.DataFrame(random_data, columns=['Column_1','Column_2','Column_3'])
print(df)

Prepare data for plotting

new_df = pd.concat(
    [
        pd.DataFrame(
            {
                "x": [i + j * 10 - 1 for i in range(1, len(df[col]) + 1)],
                "value": df[col],
                "label": col,
            }
        )
        for j, col in enumerate(df.columns)
    ]
).reset_index(drop=True)
print(new_df)

Plot

from matplotlib import pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=1)

ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)

# Position labels on x-axis 
ax.set_xticks(
    ticks=[
        new_df.loc[new_df["label"] == label, "x"].median()
        for label in new_df["label"].unique()
    ]
)
ax.set_xticklabels(new_df["label"].unique(), fontsize=12)

Plot values

for label in mean_df["label"].unique():
    err = ax.errorbar(x = mean_df.loc[mean_df["label"] == label, "x"], 
                      y = mean_df.loc[mean_df["label"] == label, "value"],
                      yerr=std_df.loc[std_df["label"] == ss, "value"],
                      marker='.', color = "Black",
                     ls='none',
                        ms=13, label='example')

    ax.legend(handles=[err], 
              loc='upper left' ,bbox_to_anchor=(1, 1)  )

plt.show()

It shows enter image description here

And this is my expect answer.(the value is yerror) enter image description here

Megan
  • 541
  • 1
  • 3
  • 14

0 Answers0