如何绘制单个数据点? [英] How to plot one single data point?

查看:104
本文介绍了如何绘制单个数据点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来画一条线和一个点:

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

如何显示单个数据点?

推荐答案

绘制单个数据点时,不能使用线进行绘制.考虑到这一点很明显,因为在绘制线时实际上是在 个数据点之间绘制图,因此,如果只有一个数据点,则没有任何东西可以连接线.

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 between data 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.

目前您正在使用

ax = point.plot(x='x', y='y', ax=ax, style='r-', label='point')

进行绘图.这将产生一条红线(r代表红色,-代表线条).如果使用下面的代码,则会得到蓝色的十字(蓝色的b,十字的x).

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

ax = point.plot(x='x', y='y', ax=ax, style='bx', label='point')

pandas内部使用matplotlib进行绘图,您可以在表此处.要在不同样式之间进行选择(例如,如果您在有多个数据点时不希望使用标记),则可以只检查数据集的长度,然后使用适当的样式.

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.

这篇关于如何绘制单个数据点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆