简体   繁体   English

如何在 Python 中使用 matplotlib 将第二个子 plot 放在第一个子 plot 之上?

[英]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。如何修改代码以使标记出现在前面?

The first code worked for me itself after I ran it in a separate virtual environment (not base environment) and after starting a fresh terminal.在我在单独的虚拟环境(不是基础环境)中运行它并启动一个新终端后,第一个代码对我自己有效。 在此处输入图像描述

暂无
暂无

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

相关问题 如何在 matplotlib.pyplot 的第一个图后面添加第二个图? 它要么在顶部,要么不可见 - How do I add a second plot behind the first one in matplotlib.pyplot? It's either on top or invisible 将第二个数据系列添加到Matplotlib中的子图 - Add a second data series to a sub-plot in Matplotlib 如何在Python中绘制第一个楔形在顶部的饼图? [matplotlib] - How to plot a pie chart with the first wedge on top, in Python? [matplotlib] 如何将字符串从第一次出现的子字符串的索引切片到Python中第二次出现的子字符串? - How can I slice a string from the index of the first occurrence of a sub string to the second occurrence of a sub string in Python? 如何使用matplotlib在python中保存一个图? - How can I save a plot in python using matplotlib? 如何使用Python matplotlib绘制带有4个象限的图形或图形? - How can I draw a graph or plot with 4 quadrants using Python matplotlib? 我如何在 Python 中使用 matplotlib plot 一条最佳拟合线? - How can I plot a line of best fit using matplotlib in Python? 如何使用 Matplotlib 在 1 个 plot 中进行 plot 2 个映射? (Python) - How to plot 2 maps in 1 plot using Matplotlib? (Python) How to plot two normal distribution curves together- the second one shorter and narrower than the first one using numpy and matplotlib in Python? - How to plot two normal distribution curves together- the second one shorter and narrower than the first one using numpy and matplotlib in Python? 在 Matplotlib 中设置轮廓子图的位置 - Setting position of contour sub-plot in Matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM