I have the following dataframe:
meteo = [["January", 9.2, 13.6, 4.7, 37, 70],
["February",9.9, 14.3, 5.4, 35, 70],
["March", 11.8, 16.1, 7.4, 36, 70],
["April", 13.7, 18.0, 9.4, 40, 69],
["May", 16.9, 21.1, 12.8, 47, 70],
["June", 20.9, 24.9, 16.8, 30, 68],
["July", 23.9, 28.0, 19.8, 21, 67],
["August", 24.4, 28.5, 20.2, 62, 68],
["September", 21.7, 26.0, 17.4, 81, 70],
["October", 17.8, 22.1, 13.5, 91, 73],
["November", 13.0, 17.3, 8.6, 59, 71],
["December", 10.0, 14.3, 5.7, 40, 69]]
import pandas as pd
# Create dataframe with above data
df = pd.DataFrame(meteo)
# Drop useless column
df.drop(0, inplace = True, axis = 1)
# Rename columns
df.rename(columns = {1: "Temp_media_anual_mes", 2: "Temp_máxima_media", 3: "Temp_mínima_media", 4: "Media_lluvias_mensual", 5:"Humedad_media_rel"}, inplace = True)
df["mes"] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
Now, i would like to plot a grouped bar chart. I would like to have 5 grouped bars per month. I have tried this, but i have a little problem with the spaces between bars:
# Setting the positions and width for the bars
pos = list(range(len(df.mes)))
width = 0.25
# Plotting the bars
fig, ax = plt.subplots(figsize=(16,10))
# Create a bar with pre_score data,
# in position pos,
plt.bar(pos,
#using df['pre_score'] data,
df['Temp_media_anual_mes'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='red')
# with label the first value in first_name
#label=df['first_name'][0])
# Create a bar with mid_score data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
#using df['mid_score'] data,
df['Temp_máxima_media'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='green')
# with label the second value in first_name
#label=df['first_name'][1])
# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos],
#using df['post_score'] data,
df['Temp_mínima_media'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='blue')
# with label the third value in first_name
#label=df['first_name'][2])
plt.bar([p + width*2 for p in pos],
#using df['post_score'] data,
df['Media_lluvias_mensual'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='orange')
# with label the third value in first_name
#label=df['first_name'][2])
plt.bar([p + width*2 for p in pos],
#using df['post_score'] data,
df['Humedad_media_rel'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='purple')
# with label the third value in first_name
#label=df['first_name'][2])
# Set the y axis label
ax.set_ylabel('Amount')
# Set the chart's title
ax.set_title('Rain and temperature')
# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])
# Set the labels for the x ticks
ax.set_xticklabels(df['mes'])
# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(df['Temp_media_anual_mes'] + df['Temp_máxima_media'] + df['Temp_mínima_media'] + df["Media_lluvias_mensual"] + df["Humedad_media_rel"])] )
plt.grid()
plt.show()
This is the plot i'm getting
As you can see, it's showing 3 separate bars, and in the third one, there are 3 bars one behind another. I know the issue is in the spacing between the bars, but i don't know how to fix it. Could someone point me in the right direction please?
EDIT:
I would also like to display above of each bar, the measurements units of each plotted values. These are:
- Celsius degrees for temperatures
- mm for precipitation amounts
- % for relative humidity
Thank you very much in advance