سؤال

The following code is to draw arcs.

import numpy as np
from matplotlib import patches
import matplotlib.pyplot as plt
import math

xcenter=300
ycenter=300
angle=0
k=0
p=[100,155]
theta=[1.0471975512, 1.0471975512]
fig=plt.figure(1)
ax=fig.add_subplot(111)
plt.ion()
S=2
plt.show()
while k<S:
    thetaval=0
    if k==0:
        e1=patches.Arc((xcenter, ycenter), p[k], p[k], 0.0, 0, theta[k]*(180/math.pi))
        ax.add_patch(e1)
    elif k!=0 and k<S-1:
        e2=patches.Arc((xcenter, ycenter), p[k], p[k], 0.0, theta[k-1]*(180/math.pi), theta[k]*(180/math.pi))
        ax.add_patch(e2)
    elif k==S-1:
        e3=patches.Arc((xcenter, ycenter), p[k], p[k], 0.0, theta[k-1]*(180/math.pi), theta[k]*(180/math.pi))
        ax.add_patch(e3)
    k=k+1
plt.show(block='True')

On executing it gives the following output:

enter image description here

Where am I going wrong?

هل كانت مفيدة؟

المحلول

They are being plotted, around the coordinate 300, but it is not zooming there. From the documentation: "The arc must be used in an Axes instance—it can not be added directly to a Figure—because it is optimized to only render the segments that are inside the axes bounding box with high resolution." You are responsible for defining the axis limits.

Now, you have two options, one is to use axis:

margin = 10
plt.axis([x0 - margin, x0 + max(p) + margin , y0 - margin , y0 + max(p) + margin])

The other is to plot something invisible at the ends of your arc, so that matplotlib will keep track of it. If you compute the positions of the end of the arc [xend, yend]:

plt.scatter([x0, y0], [xend, yend], alpha=0)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top