-1

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Bobbert
  • 73
  • 8
  • This question is not reproducible without **data**. This question needs a [SSCCE](http://sscce.org/). Please see [How to provide a reproducible dataframe](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Oct 08 '21 at 21:09

1 Answers1

0

Assuming your code is correct you can use the same approach that you used to plot your mean values.

The pandas.Dataframe.plot() method is a convenience function is called matplotlib under the hood and it returns a matplotlib.axes object. This means you can do anything you can normally do with a matplotlib.axes like ax.plot([my_values_to_plot]). If you're happy with your current approach you can pass your object ax1 back to pandas.DataFrame.plot() to update your ax1 object.

ax1 = df_mean.plot(kind='bar',
                   legend=True,
                   yerr=df_std,
                   title=f"Type 1: {data_to_plot} as a function of {influence}")

ax1 = df_median.plot(kind='bar',
                   legend=True,
                   yerr=df_std,
                   title=f"Type 1: {data_to_plot} as a function of {influence}",
ax=ax1)

NB df_median and ax=ax1

Jason
  • 4,346
  • 10
  • 49
  • 75