I have a pandas dataframe df
which looks as follows:
CSP Min Max
A 107.0 111.0
B 126.0 156.0
df.to_dict()
is as follows:
{'Min': {'A': 107.0, 'B': 126.0}, 'Max': {'A': 111.0, 'B': 156.0}}
I want to plot a bar plot for A and B by hiding the part between 0 and Min, and only showing the part between Min and Max. Therefore, I chose to color the Min as white color. The code looks as follows:
fig, ax = plt.subplots(figsize = (12, 8))
df.plot(kind = "bar",
stacked = True, color = ["white", "orange"],
ax = ax)
plt.legend([])
And the resulting plot is as shown:
I also want to plot a colored range between 55 and 148. So I used plt.fill_between()
for this purpose.
fig, ax = plt.subplots(figsize = (12, 8))
ax.fill_between(x = [-1, 2],
y1 = [55, 55],
y2 = [148, 148],
color = "gray",
alpha = 0.15,
)
plt.legend([])
Now I want to combine these two into one single plot. I used the following code:
fig, ax = plt.subplots(figsize = (12, 8))
df.plot(kind = "bar",
stacked = True, color = ["white", "orange"],
ax = ax)
ax.fill_between(x = [-1, 2],
y1 = [55, 55],
y2 = [148, 148],
color = "gray",
alpha = 0.15,
)
plt.legend([])
The resulting plot is as shown:
The problem is that the white bar is overlapping the gray region which it should not. So, how can I make the bottom part of the bar transparent so that the gray region beneath it is still visible?
CodePudding user response:
You can just plot (no stack) df['Max']
with bottom
option using df['Min']
:
df['Max'].plot(kind = "bar",
color="orange",
ax = ax,
bottom=df['Min'])