Issue
I am new to plotting with Python and can't really find an answer to the question: How can I get Cartesian coordinate plane in matplotlib? By this I mean perpendicular reference lines (coordinate axis) ended up with arrows, intersecting at the origin, (0,0), with the origin at the center of the plot.
Think about a a plane for doing high school geomtery, the following is a perfect example of what I need to achieve:enter image description here
Here is my code, I need the graph to be displayed in a Cartesian coordinate system
import numpy as np # 1
import matplotlib.pyplot as plt
t = np.arange(-6, 6, 0.2)
x = (4*(t*t-1)/(t*t+1))
y = (4*t*(t*t-1)/(t*t+1))
plt.figure(figsize=(9, 5))
plt.plot(x, y, label='Циклоида')
plt.plot(0, 0, 'go', label='O(0, 0)')
plt.xlabel('x')
plt.ylabel('y')
plt.title('График параметрически заданной функции')
plt.grid()
plt.legend(loc=0)
plt.show()
enter image description here I expect
Solution
If you want your axes intersecting at the origin, (0,0), with the origin at the center of the plot then you will have to use spines (and the object-oriented interface).
import numpy as np # 1
import matplotlib.pyplot as plt
t = np.arange(-6, 6, 0.2)
x = (4*(t*t-1)/(t*t+1))
y = (4*t*(t*t-1)/(t*t+1))
fig, ax = plt.subplots( figsize=(9, 5) )
ax.plot( x, y, label='some data' )
ax.plot(0, 0, 'go', label='O(0, 0)')
ax.set_xlabel('x',loc='right')
ax.set_ylabel('y',loc='top', rotation='horizontal')
ax.set_title('Illustration of centred axes', y=1.08)
ax.grid()
ax.spines["left" ].set_position( ('data',0.0) )
ax.spines["bottom"].set_position( ('data',0.0) )
ax.spines["right" ].set_visible ( False )
ax.spines["top" ].set_visible ( False )
ax.legend(loc=0)
plt.show()
But if you want arrow heads on your axes then you will have to draw your own. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.arrow.html
Answered By - lastchance
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.