Matplotlib 用线连接散点图点 - Python [英] Matplotlib connect scatterplot points with line - Python
问题描述
我有两个列表,日期和值.我想使用 matplotlib 绘制它们.下面创建了我的数据的散点图.
I have two lists, dates and values. I want to plot them using 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)
创建一个折线图.
但我真正想要的是散点图,其中点由一条线连接.
But what I really want is a scatterplot where the points are connected by a line.
类似于在 R 中:
plot(dates, values)
lines(dates, value, type="l")
,这给了我一个点的散点图,上面覆盖着一条连接点的线.
, which gives me a scatterplot of points overlaid with a line connecting the points.
我如何在 python 中执行此操作?
How do I do this in python?
推荐答案
我认为@Evert 有正确的答案:
I think @Evert has the right answer:
plt.scatter(dates,values)
plt.plot(dates, values)
plt.show()
几乎一样
plt.plot(dates, values, '-o')
plt.show()
您可以将 -o
替换为另一个合适的格式字符串,如文档.您还可以使用 linestyle=
和 marker=
关键字参数拆分线条和标记样式的选择.
You can replace -o
with another suitable format string as described in the documentation.
You can also split the choices of line and marker styles using the linestyle=
and marker=
keyword arguments.
这篇关于Matplotlib 用线连接散点图点 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!