I'm trying to visualize data from a pandas dataframe I recently created.
In order to do that, I'm using the pandas plot function which uses matplotlib.
I want to plot the median and mean side by side in order to compare them for each level of height.
This is what I've come up with so far. This already gives me the mean with standard deviation
passed as yerr
argument
import pandas as pd
import matplotlib.pyplot as plt
data_to_plot = "Temperature"
influence = "Height"
df_mean = df.loc[df["Type"]=="Type1",[data_to_plot, influence]].groupby(influence).mean()
df_median = df.loc[df["Type"]=="Type1",[data_to_plot, influence]].groupby(influence).median()
df_std = df.loc[df["Type"]=="Type1",[data_to_plot, influence]].groupby(influence).std()
ax1 = df_mean.plot(kind='bar',
legend=True,
yerr=df_std,
title=f"Type 1: {data_to_plot} as a function of {influence}")
for i, v in enumerate(pd.Series(df_mean[data_to_plot])):
ax1.text(i, v+2, "%.2f" %v, ha="center", c="blue")
plt.ylim(-10, 150)
plt.show()
The following code gives me a plot with mean values of Temperature for each specific and discrete heights, with the mean value written over each graph box.
I would like for the median to appear in the same way as the mean, while retaining the written values and the deviation for the mean columns.
I was able to use the dataframe.describe()
function of pandas which gives me too much info and doesn't retain the numeric values added to the graph.