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(日期,值)创建折线图。
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中这样做?
3 个解决方案
#1
83
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 linestyle you prefer.
或者你喜欢的任何线型。
#2
19
For red lines an points
对于红线一点
plt.plot(dates, values, '.r-')
or for x markers and blue lines
或用于x标记和蓝线
plt.plot(dates, values, 'xb-')
#3
7
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演示
#1
83
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 linestyle you prefer.
或者你喜欢的任何线型。
#2
19
For red lines an points
对于红线一点
plt.plot(dates, values, '.r-')
or for x markers and blue lines
或用于x标记和蓝线
plt.plot(dates, values, 'xb-')
#3
7
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演示