英文:
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 :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论