'Python Cartesian plane plot and line

I want to create a cartesian plane in python that looks similar to the attached image, where I have 2 sets of coordinates e.g. (7,7) (17.4,20), and a line going through, the information I would have to create this graph would be the gradient, y-intercept and the 2 sets of coordinates. Is this possible and if so, how?



Solution 1:[1]

Matplotlib can do just about anything, if you can figure out how to spell it.

import matplotlib.pyplot as plt
import numpy as np

def f(x):
    return 1.1*x - 1

x = np.arange(-30,30)
y = f(x)

# Plot the line.

plt.plot( x, y, 'r' )

# Plot the two points.

plt.plot( 7, f(7), 'bo' )
plt.annotate( 'A', (7, f(7)-4), color='blue' )
plt.plot( 17, f(17), 'ro')
plt.annotate( 'B', (17, f(17)-4), color='red' )

# Draw the axes.

plt.axhline(0,color='black')
plt.axvline(0,color='black')

# Draw the grid lines.

plt.minorticks_on()
plt.grid( True, 'minor', markevery=2, linestyle='--' )
plt.grid( True, 'major', markevery=10 )

plt.show()

Output: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts