Matplotlib 用线连接散点图点 - Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20130227/
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-08-18 19:38:28  来源:igfitidea点击:

Matplotlib connect scatterplot points with line - Python

pythonmatplotlib

提问by brno792

I have two lists, dates and values. I want to plot them using matplotlib. The following creates a scatter plot of my data.

我有两个列表,日期和值。我想使用 matplotlib 绘制它们。下面创建了我的数据的散点图。

import matplotlib.pyplot as plt

plt.scatter(dates,values)
plt.show()

plt.plot(dates, values)creates a line graph.

plt.plot(dates, values)创建一个折线图。

But what I really want is a scatterplot where the points are connected by a line.

但我真正想要的是一个散点图,其中的点由一条线连接。

Similar to in R:

类似于在 R 中:

plot(dates, values)
lines(dates, value, type="l")

, which gives me a scatterplot of points overlaid with a line connecting the points.

,这给了我一个点的散点图,上面覆盖着一条连接点的线。

How do I do this in python?

我如何在 python 中做到这一点?

采纳答案by Hannes Ovrén

I think @Evert has the right answer:

我认为@Evert 有正确的答案:

plt.scatter(dates,values)
plt.plot(dates, values)
plt.show()

Which is pretty much the same as

这与

plt.plot(dates, values, '-o')
plt.show()

or whatever linestyleyou prefer.

或您喜欢的任何线条样式

回答by Steve Barnes

For red lines an points

对于红线点

plt.plot(dates, values, '.r-') 

or for x markers and blue lines

或用于 x 标记和蓝线

plt.plot(dates, values, 'xb-')

回答by user3756936

In addition to what provided in the other answers, the keyword "zorder" allows one to decide the order in which different objects are plotted vertically. E.g.:

除了其他答案中提供的内容之外,关键字“zorder”允许人们决定垂直绘制不同对象的顺序。例如:

plt.plot(x,y,zorder=1) 
plt.scatter(x,y,zorder=2)

plots the scatter symbols on top of the line, while

在行的顶部绘制散点符号,而

plt.plot(x,y,zorder=2)
plt.scatter(x,y,zorder=1)

plots the line over the scatter symbols.

在散点符号上绘制线。

See, e.g., the zorder demo

参见例如zorder 演示