29

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:

enter image description here

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 ?

4
  • You could get axes limits using get_ylim() and get_xlim() and then calculate the formula for the linear function you are looking for.
    – Luka
    Nov 9, 2016 at 22:01
  • I want no linear function only a straight independent line
    – Manuel
    Nov 9, 2016 at 22:04
  • Linear function is a straight independent line. This way it depends only on the axes matplotlib chooses.
    – Luka
    Nov 9, 2016 at 22:05
  • ahh oke thank you I will take a try
    – Manuel
    Nov 9, 2016 at 22:07

3 Answers 3

34

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

enter image description here

2
  • exactly what I was looking for !
    – Manuel
    Nov 10, 2016 at 21:49
  • 14
    this can be done without the additional mlines import, just using the plot interface: ax.plot([0, 1], [0, 1], color='red', transform=ax.transAxes) Jul 29, 2020 at 11:05
4

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

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

Your Answer

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

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