首页 > 解决方案 > I am trying to make a ARC diagram using python but I am not able to get the height uniform

问题描述

I am trying to make an ARC diagram using matplotlib python. But I am not able to get the heights uniform ideally height = Radius/2. I am using scipy.intepolate to smoothen my curve. So I am not able to adjust my height as per the above-mentioned information ie 'height = Radius/2'.

I want my ARC to be uniform in height as shown in the figure in the link below: https://datavizcatalogue.com/methods/images/top_images/arc_diagram.png

Below is the code I have used

import matplotlib.pyplot as plt
%matplotlib notebook
import numpy as np
from scipy import interpolate
count=[0,15,63,7,90,10]
y=[0,3,0]
plt.figure(figsize=(40,10))
x = [1,4,7]
start=x[-1]
for i in range(len(count)):

   if i==0:
      x = [1,4,7]
   else:
     x[0]=start
     x[1]=x[0]+3
     x[2]=x[1]+3

   x2 = np.linspace(x[0], x[-1], 2000)
   y2 = interpolate.pchip_interpolate(x, y, x2)
   plt.plot(x2, y2,linewidth=(0.1+(count[i]/10)),color='green',alpha=0.6)
   ax.append(x[0])
   start=x[-1]
   new_x=[x[0],x[-1]]
   new_y=[y[0],y[-1]]
   plt.plot(new_x,[0,0],color='grey',linewidth=5)
   plt.plot(new_x,new_y,"o",color='grey',mew=10,ms=20)
   plt.plot(new_x,new_y,"o",color='white',mew=10,ms=10)

Would greatly appreciate some help.

Thanks in advance.

标签: pythonmatplotlibscipy

解决方案


您可以使用以下方法在两点之间绘制圆弧:

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

from matplotlib import patches

# set the points
x1, y1 = (0., 0.)
x2, y2 = (1., 0.)

# calculate the arc
mxmy = mx, my = [(x1 + x2) / 2, (y1 + y2) / 2]
r = np.sqrt((x1 - mx)**2 + (y1 - my)**2)
width = 2 * r
height = 2 * r
start_angle = np.arctan2(y1 - my, x1 - mx) * 180 / np.pi
end_angle = np.arctan2(my - y2, mx - x2) * 180 / np.pi

# draw
arc = patches.Arc(mxmy, width, height, start_angle, end_angle)

fig, ax = plt.subplots(1,1)
ax.add_patch(arc)
ax.set_xlim(-0.1, 1.1) # you need to set the appropriate limits explicitly!
ax.set_ylim(-0.1, 1.1)
plt.show()

无耻的插头:

前段时间,我写了一个制作弧形图的小模块,专门用于比较两个网络中的连通性(嗯,不同时间点的同一个网络,真的)。我没有使用圆弧,但它可能仍然很有趣,因为它可以做其他事情,例如最小化交叉点的数量等。此外,如果你真的非常想要圆弧,交换绘制圆弧的函数将是微不足道的。你可以在这里找到回购。


推荐阅读