Search code examples
pythonpython-3.xmatplotlibscatter-plot

drawing a 45 degrees reference line as well as making x and y axis equal range


I am trying to make the following scatterplot:

enter image description here

To something that has X and Y axis equal range as well as overlaying a 45 degree reference line on the scatterplot. I have this code but produces a plot that is not my expectation. How can I fix it?

fig = plt.figure()
ax = fig.add_subplot(111)
#ax.set_aspect('equal', adjustable='box')

plt.scatter(actuals.cpu(), predictions.cpu(), cmap='viridis')
plt.xlabel('Ground Truth')
plt.ylabel('Predictions')
plt.axis('equal')
xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k', lw=3, scalex=False, scaley=False)
plt.savefig('predictions_actuals_scatterplot.png')

Here are my Python and matplotlib versions:

$ python
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'3.5.1'

enter image description here

If I just use the following code, while the origin is met by the 45 degree line, still is not exactly what I am looking for.

fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(actuals.cpu(), predictions.cpu(), cmap='viridis')
plt.xlabel('Ground Truth')
plt.ylabel('Predictions')
xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k', lw=3, scalex=False, scaley=False)
plt.savefig('predictions_actuals_scatterplot.png')

enter image description here


Solution

  • Is this what you are wanting?

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate random data.
    
    x = np.random.random(120) * 0.21 + 0.24
    y = np.random.random(120) * 1.4 - 0.2
    ticks = np.arange( -0.2, 1.2, step=0.2 )
    
    plt.scatter(x, y, cmap='viridis')
    plt.xticks( ticks )
    plt.yticks( ticks )
    plt.plot( [-.2,1.2], [-.2,1.2], linestyle='--', color='k' )
    plt.show()
    

    Output: enter image description here