How do you add a line to a scatter plot in python?

I am using python's matplotlib and want to create a matplotlib.scatter() with additional line. The line should proceed from the lower left corner to the upper right corner independent of the scatters content. A linear regression through the data, like in this post, is not what I am looking for. Also it should be dynamically and independent of the scatter input.

This should be the final plot:

How do you add a line to a scatter plot in python?

EDIT:

Doing this got me the result:

# Scatter Plot
x = data_calc_hourly.temp
y =  data_obs_hourly.temp

lineStart = data_calc_hourly.temp.min() 
lineEnd = data_calc_hourly.temp.max()  

plt.figure()
plt.scatter(x, y, color = 'k', alpha=0.5)
plt.plot([lineStart, lineEnd], [lineStart, lineEnd], 'k-', color = 'r')
plt.xlim(lineStart, lineEnd)
plt.ylim(lineStart, lineEnd)
plt.show()

Is there any better way ?

asked Nov 9, 2016 at 21:54

4

This draws a diagonal line which is independent of the scatter plot data and which stays rooted to the axes even if you resize the window:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms

x, y = np.random.random((2, 100))*2
fig, ax = plt.subplots()
ax.scatter(x, y, c='black')
line = mlines.Line2D([0, 1], [0, 1], color='red')
transform = ax.transAxes
line.set_transform(transform)
ax.add_line(line)
plt.show()

How do you add a line to a scatter plot in python?

answered Nov 9, 2016 at 22:15

unutbuunutbu

798k171 gold badges1721 silver badges1623 bronze badges

2

Besides unutbu's answer one other option is to get the limits of the axis after you ploted the data and to use them to add the line. After this you will still need to change back the axis limits as they would change with the addition of the line:

# Scatter Plot
x = data_calc_hourly.temp
y =  data_obs_hourly.temp

lineStart = data_calc_hourly.temp.min() 
lineEnd = data_calc_hourly.temp.max()  

plt.figure()
plt.scatter(x, y, color = 'k', alpha=0.5)
y_lim = plt.ylim()
x_lim = plt.xlim()
plt.plot(x_lim, y_lim, 'k-', color = 'r')
plt.ylim(y_lim)
plt.xlim(x_lim)
plt.show()

answered Mar 1, 2019 at 10:05

How do you add a line to a scatter plot in python?

João AlmeidaJoão Almeida

3,9372 gold badges18 silver badges35 bronze badges

0

I have tried updating the min and max limits for the cases where X and Y axis have different max and min data.

x = data_calc_hourly.temp
y =  data_obs_hourly.temp

calc_min = data_calc_hourly.temp.min()
calc_max = data_calc_hourly.temp.max()

obs_min = data_obs_hourly.temp.min()
obs_max = data_obs_hourly.temp.max()

lineStart = min(calc_min,obs_min)
lineEnd = max(calc_max,obs_max)

plt.figure()
plt.scatter(x, y, color = 'k', alpha=0.5)
plt.plot([lineStart, lineEnd], [lineStart, lineEnd], color = 'r')
plt.xlim(lineStart, lineEnd)
plt.ylim(lineStart, lineEnd)
plt.show()

answered Jul 18, 2021 at 14:56

JohnnyJohnny

1171 silver badge8 bronze badges


First, we can create a scatter for different data points using the scatter method, and then, we can plot the lines using the plot method.

Steps

  • Create a new figure, or activate an existing figure with figure size(4, 3), using figure() method.

  • Add an axis to the current figure and make it the current axes, create x using plt.axes().

  • Draw scatter points using scatter() method.

  • Draw line using ax.plot() method.

  • Set the X-axis label using plt.xlabel() method.

  • Set the Y-axis label using plt.ylabel() method.

  • To show the plot, use plt.show() method.

Example

import random
import matplotlib.pyplot as plt

plt.figure(figsize=(4, 3))
ax = plt.axes()
ax.scatter([random.randint(1, 1000) % 50 for i in range(100)],
[random.randint(1, 1000) % 50 for i in range(100)])
ax.plot([1, 2, 4, 50], [1, 2, 4, 50])

ax.set_xlabel('x')
ax.set_ylabel('y')

plt.show()

Output

How do you add a line to a scatter plot in python?

How do you add a line to a scatter plot in python?

Updated on 17-Mar-2021 08:48:17

  • Related Questions & Answers
  • Adding a line to a scatter plot using Python's Matplotlib
  • How to draw an average line for a scatter plot in MatPlotLib?
  • How to make a 3D scatter plot in Python?
  • How to plot additional points on the top of a scatter plot in Matplotlib?
  • How to make a scatter plot for clustering in Python?
  • How to animate a scatter plot in Matplotlib?
  • Connecting two points on a 3D scatter plot in Python and Matplotlib
  • Plot a multicolored line based on a condition in Python Matplotlib
  • Python - Draw a Scatter Plot for a Pandas DataFrame
  • How to plot scatter masked points and add a line demarking masked regions in Matplotlib?
  • How to plot a dashed line on a Seaborn lineplot in Matplotlib?
  • How do you plot a vertical line on a time series plot in Pandas?
  • Controlling the alpha value on a 3D scatter plot using Python and Matplotlib
  • Create a Scatter Plot with SeaBorn – Python Pandas
  • How can Seaborn library be used to display a Scatter Plot in Python?

How do you add a line to a scatter plot?

Select the data that you want to plot in the line chart. On the Insert tab, in the Charts group, click Line. Click Line with Markers. Click the chart area of the chart.

How do I add a line in MatPlotLib?

The axhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis..
y: Position on Y axis to plot the line, It accepts integers..
xmin and xmax: scalar, optional, default: 0/1. ... .
color: color for the line, It accepts a string..

How do you draw a line on a graph in Python?

Simple Line Plots.
%matplotlib inline import matplotlib.pyplot as plt plt. style. use('seaborn-whitegrid') import numpy as np. ... .
fig = plt. figure() ax = plt. axes() ... .
In [3]: fig = plt. figure() ax = plt. ... .
In [4]: plt. plot(x, np. ... .
In [5]: plt. plot(x, np. ... .
plt. plot(x, x + 0, '-g') # solid green plt. ... .
In [9]: plt. ... .
In [10]: plt..

How do I insert a horizontal line in MatPlotLib?

In matplotlib, if you want to draw a horizontal line with full width simply use the axhline() method. You can also use the hlines() method to draw a full-width horizontal line but in this method, you have to set xmin and xmax to full width.