YohanRoth
YohanRoth

Reputation: 3253

Python - Combine two bar plots in one plot window

I need to add the following information

score_to_plot = pd.DataFrame({"a":["c1", "c2", "c3", "c4"], 
                              "max_score":[69,77,87,99],
                              "min_score":[55, 72, 83, 94]})

as a "box plot", such that left side of a box starts at the min_score and right_side of a box ends at the max_score, to the plot below. Having the scores range to be as an additional axis at the top.

Note that here bottom and top ranges are about the same, but in my actual work it is not the case and I cannot use one axis range.

f, ax = plt.subplots()
sns.set_color_codes("pastel")
sns.barplot(x="time", y="a", data={"a":["c1", "c2", "c3", "c4"], "time":[50,70,85,150]},
            label="time_total", color="b")


sns.set_color_codes("muted")
sns.barplot(x="time", y="a", data={"a":["c1", "c2", "c3", "c4"], "time":[20,40,65,135]},
            label="time_productive", color="b")


ax.legend(ncol=2, loc="lower right", frameon=True)
ax.set(ylabel="",
       xlabel="Productive time vs total time vs score")
sns.despine(left=True, bottom=True)

How would I do this? Thank you for help!

enter image description here

An approximate idea of what I want as a result

enter image description here

Upvotes: 1

Views: 432

Answers (1)

SKPS
SKPS

Reputation: 5836

I have saved score_to_plot as df. With following addition:

df["widths"] = df["max_score"]-df["min_score"]
ax2 = ax.twiny()
ax2.barh(df.a, df.widths, left=df.min_score, color="grey")
plt.xlim([0,100])
ax.legend(loc='lower left')

I get this. Does it help? enter image description here

Upvotes: 1

Related Questions