如何在matplot python中绘制单个点

Xry*_*pto 22 python matplotlib

我想在我的图表上绘制一个点,但似乎他们都需要绘制为列表或方程式.

我需要绘制像ax.plot(x,y)的图,并且我的图上的x,y坐标上会出现一个点.

这是我的代码

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.set_xticks(numpy.arange(0,grid[0] + 20,20))
ax.set_yticks(numpy.arange(0,grid[1] + 20,20))
ax.plot(105, 200)
plt.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

Tre*_*ney 25

  • matplotlib.pyplot.plotmatplotlib.axes.Axes.plot绘图与线条和/或标记y的比较x
  • 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.10, matplotlib 3.5.1,seaborn 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

西博恩

轴级图

sns.lineplot(x=[1], y=[1], marker='o', markersize=10, color='r')
Run Code Online (Sandbox Code Playgroud)
sns.scatterplot(x=[1], y=[1], s=100, color='r')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

图形级图

sns.relplot(kind='line', x=[1], y=[1], marker='o', markersize=10, color='r')
Run Code Online (Sandbox Code Playgroud)
sns.relplot(kind='scatter', x=[1], y=[1], s=100, color='r')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


sco*_*tle 22

这对我有用:

plt.plot(105,200,'ro') 
Run Code Online (Sandbox Code Playgroud)

  • r 表示红色,o 表示点。 (16认同)
  • plt.plot(105,200,'ro',label ='Point A'); plt.legend(); (2认同)