多线图

huangapple go评论54阅读模式
英文:

Multiple line graph

问题

我有一个看起来像这样的数据框架:

No._trees  prop._robin    prop._dove
1          0.5            0.6
2          0.6            0.2

无论是知更鸟还是鸽子,平均鸟的数量都只会在0到1之间。我想创建一个图表,其中树的数量将是x轴变量,y轴变量将在0到1之间,并且会有两条线,一条是知更鸟的,一条是鸽子的。我遇到困难,因为我读过的所有示例都将y变量视为数据框列,而在我的情况下,我只希望它在0到1之间。

可复制的数据框:

d = {'No._trees': [1, 2], 'prop._robin': [0.5, 0.6], 'prop._dove':[0.6,0.2]}
df = pd.DataFrame(data=d)
英文:

I have a dataframe that looks like this:

No._trees  prop._robin    prop._dove
1          0.5            0.6
2          0.6            0.2

The mean number of birds whether it is robins or doves will only ever be between 0 and 1. I want to create a graph where the number of trees would be the x-axis variable, the y-axis variable would go between 0 and 1 and there would be two lines, one for the robins and one for the doves. I am struggling because all of the examples I have read have the y-variable as a dataframe column, whereas in my case I just want it to go from 0 to 1.

Reproducible dataframe:

d = {'No._trees': [1, 2], 'prop._robin': [0.5, 0.6], 'prop._dove':[0.6,0.2]}
df = pd.DataFrame(data=d)

答案1

得分: 2

以下是翻译好的部分:

一种选择是在之后调整df的标题并plot生成的DataFrame:

out = (
    df.set_index("No._trees")
        .rename(lambda x: x.removeprefix("prop._"), axis=1)
)

out.plot(ylim=(0, 1), xticks=out.index, legend=True); # 如果需要,可以添加更多的kwargs

输出:

多线图

英文:

One option is to adjust the df header's and plot the resulting DataFrame afterwards :

out = (
    df.set_index("No._trees")
        .rename(lambda x: x.removeprefix("prop._"), axis=1)
)

out.plot(ylim=(0, 1), xticks=out.index, legend=True); # add more kwargs if needed

Output :

多线图

huangapple
  • 本文由 发表于 2023年6月18日 19:31:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定