46

I have the following code to plot a line and a point:

df = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 6]})
point = pd.DataFrame({'x': [2], 'y': [5]})
ax = df.plot(x='x', y='y', label='line')
ax = point.plot(x='x', y='y', ax=ax, style='r-', label='point')

How do I get the single data point to show up?

Plot with line and no point

2
  • 1
    Ok, thanks for your answer. Does a plotting method for single data points even exist? if so, can you name it? thanks! Commented Jan 5, 2015 at 13:04
  • Peter, thanks for your question. I edited it to make it a reproducible minimal example. If possible, please try to do so in the future. I also removed the time-series and forecasting tags (this was not a modeling question), and added the pandas tag (since it involves plotting with pandas).
    – Max Ghenis
    Commented Feb 28, 2018 at 13:36

4 Answers 4

117

To plot a single point you can do something like this:

plt.plot([x], [y], marker='o', markersize=3, color="red")
3
  • 3
    Note: you can also omit the brackets.
    – Karlo
    Commented Nov 9, 2017 at 17:33
  • 5
    I think part of what OP wants is to have the point also show up in the legend, given their use of the label argument in pandas plotting. Is there a way to do that here? In the new reproducible example, this doesn't work: plt.plot(point['x'], point['y'], marker='o', markersize=3, color='red', label='point').
    – Max Ghenis
    Commented Feb 28, 2018 at 13:49
  • This removes the axes for a plot using a different set of points. Commented Dec 29, 2021 at 22:30
11

When plotting a single data point, you cannot plot using lines. This is obvious when you think about it, because when plotting lines you actually plot between data points, and so if you only have one data point then you have nothing to connect your line to.

You can plot single data points using markers though, these are typically plotted directly on the data point and so it doesn't matter if you have only one data point.

At the moment you're using

ax = point.plot(x='x', y='y', ax=ax, style='r-', label='point')

to plot. This produces a red line (r for red, - for line). If you use the following code then you'll get blue crosses (b for blue, x for a cross).

ax = point.plot(x='x', y='y', ax=ax, style='bx', label='point')

pandas uses matplotlib internally for plotting, you can find the various style arguments in the tables here. To choose between the different styles (if, for example, you didn't want markers when you have multiple data points) then you could just check the length of the dataset and then use the appropriate style.

1
  • You also don't need to reassign ax.
    – Max Ghenis
    Commented Feb 28, 2018 at 13:40
9

Another issue that exists when using the .plot(..) method is that the legend is displayed with lines and not dots. To fix this issue, I would recommend to use plt.scatter(..) as such:

df = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 6]})
point = pd.DataFrame({'x': [2], 'y': [5]})

fig, axes = plt.subplots(1, 2, figsize=(20, 5))

# OP VERSION
df.plot('x', 'y', ax=axes[0], label='line')
point.plot('x', 'y', ax=axes[0], style='r-', label='point')

# MY VERSION
df.plot('x', 'y', ax=axes[1], label='line')
axes[1].scatter(point['x'], point['y'], marker='o', color='r', label='point')
axes[1].legend(loc='upper left')

I obtain this result, with on the left, OP's method, and on the right, my method: comparison_of_plot_methods

1

In the last line of the code in question, replace style='-r' with kind='scatter':

ax = point.plot(x='x', y='y', ax=ax, kind='scatter', label='point')

enter image description here

You can optionally add a color argument to the call to point.plot:

ax = point.plot(x='x', y='y', ax=ax, kind='scatter', label='point', color='red')

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.