简体   繁体   English

Matplotlib 用线连接散点图点 - Python

[英]Matplotlib connect scatterplot points with line - Python

I have two lists, dates and values.我有两个列表,日期和值。 I want to plot them using matplotlib.我想使用 matplotlib 绘制它们。 The following creates a scatter plot of my data.下面创建了我的数据的散点图。

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 中做到这一点?

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()

You can replace -o with another suitable format string as described in the documentation .您可以将-o替换为其他合适的格式字符串,如文档中所述。 You can also split the choices of line and marker styles using the linestyle= and marker= keyword arguments.您还可以使用linestyle=marker=关键字参数拆分线条和标记样式的选择。

For red lines an points对于红线点

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

or for x markers and blue lines或 x 标记和蓝线

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

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

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, eg, the zorder demo参见例如zorder 演示

They keyword argument for this is marker , and you can set the size of the marker with markersize .他们的关键字参数是marker ,您可以使用markersize设置标记的大小。 To generate a line with scatter symbols on top:要生成顶部带有散点符号的线:

plt.plot(x, y, marker = '.', markersize = 10)

To plot a filled spot, you can use marker '.'要绘制填充点,您可以使用标记'.' or 'o' (the lower case letter oh).'o' (小写字母哦)。 For a list of all markers, see:有关所有标记的列表,请参阅:
https://matplotlib.org/stable/api/markers_api.html https://matplotlib.org/stable/api/markers_api.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM