如何在matplotlib中绘制单个点

68

我想在我的图表上绘制一个单独的点,但似乎它们都需要作为列表或方程式来绘制。

我需要像 ax.plot(x, y) 这样绘制,并且在我的图表上使用 xy 坐标出现一个点。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import numpy
fig = plt.figure()
plt.xlabel('Width')
plt.ylabel('Height')
ax = fig.gca()
ax.plot(105, 200)
plt.grid()
plt.show()

输入图像描述这里

2个回答

113

这对我有用:

plt.plot(105,200,'ro') 

13
r代表红色,o代表圆点。 - matthewpark319

24
  • matplotlib.pyplot.plotmatplotlib.axes.Axes.plot可以绘制yx之间的线条和/或标记。
  • ax.plot(105, 200)尝试绘制一条线,但是需要两个点才能绘制一条线
    • plt.plot([105, 110], [200, 210])
  • 第三个位置参数由线条类型、颜色和/或标记组成
    • 'o'可用于仅绘制标记。
      • 指定marker='o'与位置参数不同。
    • 'ro'分别指定颜色和标记
    • '-o''-ro'将在提供两个或多个xy值时绘制线条和标记。
  • matplotlib.pyplot.scattermatplotlib.axes.Axes.scatter也可用于添加单个或多个点
  • python 3.10matplotlib 3.5.1seaborn 0.11.2中测试通过
import matplotlib.pyplot as plt

fig, ax = plt.subplots(3, 1, figsize=(8, 10), tight_layout=True)

# single point
ax[0].plot(105, 110, '-ro', label='line & marker - no line because only 1 point')
ax[0].plot(200, 210, 'go', label='marker only')  # use this to plot a single point
ax[0].plot(160, 160, label='no marker - default line - not displayed- like OP')
ax[0].set(title='Markers - 1 point')
ax[0].legend()

# two points
ax[1].plot([105, 110], [200, 210], '-ro', label='line & marker')
ax[1].plot([105, 110], [195, 205], 'go', label='marker only')
ax[1].plot([105, 110], [190, 200], label='no marker - default line')
ax[1].set(title='Line & Markers - 2 points')
ax[1].legend()

# scatter plot
ax[2].scatter(x=105, y=110, c='r', label='One Point')  # use this to plot a single point
ax[2].scatter(x=[80, 85, 90], y=[85, 90, 95], c='g', label='Multiple Points')
ax[2].set(title='Single or Multiple Points with using .scatter')
ax[2].legend()

enter image description here

Seaborn

  • seaborn 是一个基于 matplotlib 的高级 API,提供了绘制单个点的额外选项。
  • sns.lineplotsns.scatterplot 是轴级别的图表。
  • sns.relplot 是一个图形级别的图表,具有 kind= 参数。
    • kind='line' 会传递给 sns.lineplot
    • kind='scatter' 会传递给 sns.scatterplot
  • 在以下情况下,x=y= 必须作为向量传递。

轴级别绘图

sns.lineplot(x=[1], y=[1], marker='o', markersize=10, color='r')

sns.scatterplot(x=[1], y=[1], s=100, color='r')

enter image description here

图形级别的绘图

sns.relplot(kind='line', x=[1], y=[1], marker='o', markersize=10, color='r')

sns.relplot(kind='scatter', x=[1], y=[1], s=100, color='r')

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接