简体   繁体   English

在同一图Python中覆盖不同的图类型

[英]Overlaying different plots types in the same figure Python

I need to put both a boxplot and a bar plot in the same graph. 我需要在同一个图表中放置一个boxplot和一个条形图。 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 = 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. 现在,我想重叠一个方框图,其中A列和B列以及一个条形图与每列的平均值重叠。

How I can overlap them using matplotlib once they are different types? 一旦它们是不同的类型,我如何使用matplotlib重叠它们?

thanks, 谢谢,

Get the handle for the axes, the plot the second on that axes using ax=ax . 获取轴的手柄,使用ax=ax在该轴上绘制第二个图。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM