简体   繁体   English

如何在 matplotlib 中绘制单个点

[英]How to plot a single point in matplotlib

I'd like to plot a single point on my graph, but it seems like they all need to plot as either a list or equation.我想在我的图表上绘制一个点,但似乎它们都需要绘制为列表或方程。

I need to plot like ax.plot(x, y) and a dot will be appeared at my x , y coordinates on my graph.我需要像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()

在此处输入图像描述

这对我有用:

plt.plot(105,200,'ro') 
  • matplotlib.pyplot.plot and matplotlib.axes.Axes.plot plots y versus x as lines and/or markers. matplotlib.pyplot.plotmatplotlib.axes.Axes.plotyx绘制为线条和/或标记。
  • ax.plot(105, 200) attempts to draw a line, but two points are required for a line ax.plot(105, 200)试图画一条线,但是一条线需要两个点
    • plt.plot([105, 110], [200, 210])
  • A third positional argument consists of line type, color, and/or marker第三个位置参数由线条类型、颜色和/或标记组成
    • 'o' can be used to only draw a marker. 'o'只能用于绘制标记。
      • Specifying marker='o' does not work the same as the positional argument.指定marker='o'与位置参数不同。
    • 'ro' specifies color and marker, respectively 'ro'分别指定颜色和标记
    • '-o' or '-ro' will draw a line and marker if two or more x and y values are provided.如果提供了两个或多个xy值,“-o '-o''-ro'将绘制一条线和标记。
  • matplotlib.pyplot.scatter and matplotlib.axes.Axes.scatter can also be used to add single or multiple points matplotlib.pyplot.scattermatplotlib.axes.Axes.scatter也可用于添加单个或多个点
  • Tested in python 3.10 , matplotlib 3.5.1 , seaborn 0.11.2python 3.10matplotlib 3.5.1seaborn 0.11.2
import matplotlib.pyplot as plt

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

# 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()

fig.tight_layout()

在此处输入图像描述

Seaborn海博恩

  • seaborn is a high-level api for matplotlib , and offers additional options for plotting single points. seabornmatplotlib的高级 api,并提供了用于绘制单点的附加选项。
  • sns.lineplot and sns.scatterplot are axes-level plots. sns.lineplotsns.scatterplot是轴级图。
  • sns.relplot is a figure-level plot, with a kind= parameter. sns.relplot是一个图形级别的图,带有kind=参数。
    • kind='line' passes to sns.lineplot kind='line'传递给sns.lineplot
    • kind='scatter' passes to sns.scatterplot kind='scatter'传递给sns.scatterplot
  • x= and y= must be passed as a vector in the following cases.在以下情况下, x=y=必须作为向量传递。

axes-level plots轴级图

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

在此处输入图像描述

figure-level plots图形级图

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

在此处输入图像描述

here is an example on how to do it with numpy on a 2d plot这是一个关于如何在 2d 绘图上使用 numpy 的示例

import numpy as np
import matplotlib.pyplot as plt

# column 1 Age
# Column 2 Weight
x = np.array([[10, 40],
              [11, 35],
              [12, 40],
              [13, 41],
              [13, 70],
              [15, 60],
              [19, 64],
              [15, 60],
              [20, 80],
              [40, 100],
              [56, 85]])

for i in x:
    plt.plot(i[0], i[1], 'ro')
plt.xlabel("Age")
plt.ylabel("Weight")
plt.show()

显示 x 数据点的二维图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM