pandas DataFrame 上散点图的连接线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37581145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:19:45  来源:igfitidea点击:

Connect line of scatter plot on pandas DataFrame

pythonpandasmatplotlib

提问by rkjt50r983

I'd like to add lines between dots of a scatter plot drawn by pandas. I tried this, but does not work. Can I put lines on a scatter plot?

我想在Pandas绘制的散点图的点之间添加线。我试过这个,但不起作用。我可以在散点图上放线吗?

pd.DataFrame([[1,2],[10,20]]).plot(kind="scatter", x=0, y=1, style="-")
pd.DataFrame([[1,2],[10,20]]).plot.scatter(0,1,style="-")

enter image description here

在此处输入图片说明

回答by Neapolitan

A solution is to replot the line on top of the scatter:

一个解决方案是重新绘制散点顶部的线:

df = pd.DataFrame([[1,2],[10,20]])
ax = df.plot.scatter(x=0, y=1, style='b')
df.plot.line(x=0, y=1, ax=ax, style='b')

In this case, forcing points and lines both to be blue.

在这种情况下,强制点和线都为蓝色。

If you don't need the properties of the scatter plot such as value dependent colours and sizes, just use a line plot with circles for the points:

如果您不需要散点图的属性,例如值相关的颜色和大小,只需使用带圆圈的线图作为点:

df.plot.line(x=0, y=1, style='-o')