Python 如何绘制单个数据点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27779845/
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
How to plot one single data point?
提问by Peter Knutsen
I have the following code to plot a line and a point:
我有以下代码来绘制一条线和一个点:
df = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 6]})
point = pd.DataFrame({'x': [2], 'y': [5]})
ax = df.plot(x='x', y='y', label='line')
ax = point.plot(x='x', y='y', ax=ax, style='r-', label='point')
How do I get the single data point to show up?
如何显示单个数据点?
采纳答案by Ffisegydd
When plotting a single data point, you cannot plot using lines. This is obvious when you think about it, because when plotting lines you actually plot betweendata points, and so if you only have one data point then you have nothing to connect your line to.
绘制单个数据点时,不能使用线条绘制。仔细想想这很明显,因为在绘制线条时实际上是在数据点之间绘制,因此如果您只有一个数据点,那么您就没有任何东西可以连接线。
You can plot single data points using markers though, these are typically plotted directly on the data point and so it doesn't matter if you have only one data point.
不过,您可以使用标记绘制单个数据点,这些通常直接绘制在数据点上,因此如果您只有一个数据点也没关系。
At the moment you're using
目前您正在使用
ax = point.plot(x='x', y='y', ax=ax, style='r-', label='point')
to plot. This produces a red line (r
for red, -
for line). If you use the following code then you'll get blue crosses (b
for blue, x
for a cross).
情节。这会产生一条红线(r
对于红色,-
对于线)。如果您使用以下代码,那么您将获得蓝色十字(b
蓝色,x
十字)。
ax = point.plot(x='x', y='y', ax=ax, style='bx', label='point')
pandas
uses matplotlib
internally for plotting, you can find the various style arguments in the tables here. To choose between the different styles (if, for example, you didn't want markers when you have multiple data points) then you could just check the length of the dataset and then use the appropriate style.
pandas
在matplotlib
内部用于绘图,您可以在此处的表格中找到各种样式参数。要在不同的样式之间进行选择(例如,如果您在有多个数据点时不需要标记),那么您只需检查数据集的长度,然后使用适当的样式即可。
回答by Andrei Pokrovsky
To plot a single point you can do something like this:
要绘制单个点,您可以执行以下操作:
plt.plot([x], [y], marker='o', markersize=3, color="red")