Xrypto
Xrypto

Reputation: 821

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.

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

enter image description here

Upvotes: 82

Views: 218540

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62393

  • matplotlib.pyplot.plot and matplotlib.axes.Axes.plot plots y versus x as lines and/or markers.
  • ax.plot(105, 200) attempts to draw a line, but two points are required for a line
    • 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.
      • Specifying marker='o' does not work the same as the positional argument.
    • 'ro' specifies color and marker, respectively
    • '-o' or '-ro' will draw a line and marker if two or more x and y values are provided.
  • matplotlib.pyplot.scatter, plt.scatter(x, y), and matplotlib.axes.Axes.scatter, ax.scatter(x, y), can also be used to add single or multiple points
  • Tested in 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()

enter image description here

Seaborn

  • seaborn is a high-level api for matplotlib, and offers additional options for plotting single points.
  • sns.lineplot and sns.scatterplot are axes-level plots.
  • sns.relplot is a figure-level plot, with a kind= parameter.
    • kind='line' passes to sns.lineplot
    • kind='scatter' passes to sns.scatterplot
  • x= and y= must be passed as a vector in the following cases.

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

enter image description here

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

enter image description here

Upvotes: 28

scottlittle
scottlittle

Reputation: 20822

This worked for me:

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

Upvotes: 127

Related Questions