Guga
Guga

Reputation: 349

Overlaying different plots types in the same figure Python

I need to put both a boxplot and a bar plot in the same graph. I have a dataframe like this:

Suppose I recebe the data like this:

df = pd.DataFrame([np.random.normal(0,1,10), \
                   np.random.normal(0,1,10)],
                  index=["A", "B"])

In order to have the index as columns I transpose the df

df = df.transpose()

Now I want to overlap a box plot with for columns A and B and a Bar Plot with the mean of each column.

How I can overlap them using matplotlib once they are different types?

thanks,

Upvotes: 4

Views: 3386

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Get the handle for the axes, the plot the second on that axes using ax=ax.

ax = df.plot.box()
_ = df.T.plot.bar(ax=ax)
plt.show()

enter image description here

Upvotes: 6

Related Questions