[英]How can I bring second sub plot on top of first sub plot using matplotlib in Python?
I have a dataframe df
which looks as follows:我有一个 dataframe df
,如下所示:
Min Average Max
Score 30 55 80
df.to_dict
is as follows: df.to_dict
如下:
{'Min': {'Score': 30}, 'Average': {'Score': 55}, 'Max': {'Score': 80}}
I want to plot a bar plot starting from Min to Max, and add the Average in the form of a marker in the bar.我想 plot 一根柱线 plot 从最小值到最大值,并在柱线中以标记的形式添加平均值。 I wrote the following code:我写了以下代码:
fig, ax = plt.subplots()
df["Max"].plot(kind = "bar",
bottom = df["Min"], ax = ax,
width = 0.1)
ax.scatter(x = 0,
y = df["Average"],
marker = "_",
s = 10000,
color = "red")
plt.ylim(0, 120)
This returned me the plot as shown:这返回了 plot,如下所示:
As observed, the scatter plot or the horizontal red line as marker is hidden by the blue bar.正如观察到的那样,散点图 plot 或作为标记的水平红线被蓝色条隐藏了。 I want to bring it on the front.我想把它放在前面。
I brought the line of code regarding scatter plot before the bar plot as follows:我把关于散点图 plot 的代码行放在 plot 之前,如下所示:
fig, ax = plt.subplots()
plt.scatter(x = 0,
y = df["Average"],
marker = "_",
s = 10000,
color = "red")
df["Max"].plot(kind = "bar",
bottom = df["Min"], ax = ax,
width = 0.1)
plt.ylim(0, 120)
But I am still getting the same plot. How can I modify the code such that the marker can come on the front?但我仍然得到相同的 plot。如何修改代码以使标记出现在前面?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.