Reputation:
Consider the following example: This code will create an circle with center 180, 150 and radius 15. Further a point will be added at position a,b.
%matplotlib inline
import matplotlib.pyplot as plt
import math
import numpy as np
def get_circle(x, y, r):
""" Get a cirlce with a specific position and radius"""
result = plt.Circle((x, y), r, color='k', fill=False)
return result
def rpoint(x, y, r, deg):
""" Get a point on a radius at deg degrees."""
rang = np.deg2rad(deg)
x1 = x + r * np.sin(rang)
y1 = y + r * np.cos(rang)
return (x1, y1)
fig, ax = plt.subplots(1, 1, figsize=(6,6))
ax.set_aspect('equal', adjustable='datalim')
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
x1, y1, r = 180, 150, 15
ax.add_patch(get_circle(x1, y1, r))
a, b = rpoint(x1, y1, r, 180)
plt.plot(a,b,'ok')
Now i want simply to add an arrow that shows in the direction of the black dot with half the radius. From the docs i tried to add the desired arrow in the following way.
plt.arrow(x1, y1, a, b)
or
plt.arrow(x1, y1, x1 + abs(x1 - a), y1 + abs(y1 - b))
Both ways result in the same result:
The arrows direction and length is completely wrong even though i used the correct coordinates a,b. What am I doing wrong?
Upvotes: 0
Views: 2420
Reputation: 1511
The second and third arguments of plt.arrow
are dx
and dy
and not the coordinates of the ending points (from the doc).
Therefore use plt.arrow(x1, y1, a - x1, (b - y1)/2)
. Note that the division by two comes from the fact that you want the arrow to end at half the radius.
Upvotes: 1
Reputation: 11657
I don't fully understand what's happened, but the origin of your circle is (0, 0)
, so you need to give it a negative value. This works:
plt.arrow(x1, y1, 0, -r / 2, head_width=1, head_length=1)
Or in terms of your variables:
plt.arrow(x1, y1, a - x1, (b - y1) / 2, head_width=1, head_length=1)
Upvotes: 2