I am using pandas.plot
to create a grouped bar chart. I'd like to overlay that chart with two different markers per data point. IRL the markers represent the same data from prior time periods. I know how to create the markers using a kind='line'
and linestyle='None'
but I don't know how to get them to align with the bars. The desired output would have a colored bar for each data point (from df
in the MRE below) and aligned with that bar would be two distinguishable markers representing df2
and df3
.
I thought of creating another bar chart that only uses markers, but AFAIK that isn't possible. I also went down a rabbit hole of using yerr
as in this example:
How can I add markers on a bar graph in python?
But it doesn't seem possible to produce two separate markers for the upper/lower error, which is necessary here.
Here is a simple example of what I'm trying to achieve, with the only problem being the x-alignment of the markers.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
df_data = {'A':np.random.randint(20, 50, 5),
'B':np.random.randint(10, 50, 5),
'C':np.random.randint(30, 50, 5),
'D':np.random.randint(30, 100, 5)}
df2_data = {'A':np.random.randint(20, 50, 5),
'B':np.random.randint(10, 50, 5),
'C':np.random.randint(30, 50, 5),
'D':np.random.randint(30, 100, 5)}
df3_data = {'A':np.random.randint(20, 50, 5),
'B':np.random.randint(10, 50, 5),
'C':np.random.randint(30, 50, 5),
'D':np.random.randint(30, 100, 5)}
df = pd.DataFrame(data=df_data)
df2 = pd.DataFrame(data=df2_data)
df3 = pd.DataFrame(data=df3_data)
fig = plt.figure()
bar_colors = ['red', 'blue', 'green', 'goldenrod']
ax = fig.gca()
df2.plot(kind='line', ax=ax, linestyle='None', marker='^',
color=bar_colors,
markerfacecolor='white', legend=False)
df3.plot(kind='line', ax=ax, linestyle='None', marker='o',
color=bar_colors,
markerfacecolor='white', legend=False)
df.plot(kind='bar', ax=ax, color=bar_colors, width=0.8, rot=0)
plt.show()