How to plot a single point in matplotlib

PythonMatplotlib

Python Problem Overview


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

Python Solutions


Solution 1 - Python

This worked for me:

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

Solution 2 - Python

  • matplotlib.pyplot.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 can also be used to add single or multiple points
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()

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXryptoView Question on Stackoverflow
Solution 1 - PythonscottlittleView Answer on Stackoverflow
Solution 2 - PythonTrenton McKinneyView Answer on Stackoverflow