[英]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)
那样进行绘图,并且我的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.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.plot
和matplotlib.axes.Axes.plot
将y
与x
绘制为线条和/或标记。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])
'o'
can be used to only draw a marker. 'o'
只能用于绘制标记。
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.x
和y
值,“-o '-o'
或'-ro'
将绘制一条线和标记。matplotlib.pyplot.scatter
and matplotlib.axes.Axes.scatter
can also be used to add single or multiple points matplotlib.pyplot.scatter
和matplotlib.axes.Axes.scatter
也可用于添加单个或多个点python 3.10
, matplotlib 3.5.1
, seaborn 0.11.2
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))
# 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
is a high-level api for matplotlib
, and offers additional options for plotting single points. seaborn
是matplotlib
的高级 api,并提供了用于绘制单点的附加选项。sns.lineplot
and sns.scatterplot
are axes-level plots. sns.lineplot
和sns.scatterplot
是轴级图。
sns.lineplot
has keyword arguments, which are passed to matplotlib.axes.Axes.plot
sns.lineplot
有关键字参数,它们被传递给matplotlib.axes.Axes.plot
sns.scatterplot
has keyword arguments, which are passed to matplotlib.axes.Axes.scatter
sns.scatterplot
有关键字参数,它们被传递给matplotlib.axes.Axes.scatter
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=
必须作为向量传递。sns.lineplot(x=[1], y=[1], marker='o', markersize=10, color='r')
sns.scatterplot(x=[1], y=[1], s=100, color='r')
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.