天天看点

python如何画贝塞尔曲线_​用Python把图做的好看点:用Matplotlib画个Circos和弦图...

一直觉得circos图 很好看,最近尝试了一下 对中间关系曲线的 绘制

主要利用的数学工具是 贝塞尔曲线

但是对贝塞尔曲线中间的控制点取值 还是不太满意 造成部分曲线看着有点奇怪

成品大概是这样

代码如下:

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import random

Start1=[0 for i in range(50)]

for i in range(60):

Start1.append(5/np.power(2,0.2))

for i in range(80):

Start1.append(-5/np.power(2,0.2))

df=pd.DataFrame(Start1,columns=['Startx'])

df['Starty']=np.power(25-np.power(df['Startx'],2),0.5)*-1

df['Endx']=[random.uniform(-5,5.0) for i in range(len(df))]

df['Endy']=np.power(25-np.power(df['Endx'],2),0.5)*[random.sample([1,-1], 1)[0] for i in range(len(df))]

df['']=np.power(25-np.power(df['Endx'],2),0.5)

df['斜率1']=(df['Starty']-df['Endy'])/(df['Startx']-df['Endx'])

df['斜率2']=-1/df['斜率1']

df['中心点x']=(df['Startx']+df['Endx'])/2

df['中心点y']=(df['Starty']+df['Endy'])/2

df['x轴移动']=df['中心点x']-df['中心点x']*(0.05)

df['y轴移动']=df['x轴移动']*df['斜率2']

df['控制点x']=df['中心点x']-df['x轴移动']

df['控制点y']=df['中心点y']-df['y轴移动']

df['r1']=np.power(df['Startx']*df['Startx']+df['Starty']*df['Starty'],1/2)

df['theta1']=np.arctan2(df['Starty'],df['Startx'])

df['r2']=np.power(df['Endx']*df['Endx']+df['Endy']*df['Endy'],1/2)

df['theta2']=np.arctan2(df['Endy'],df['Endx'])

fig=plt.figure(figsize=[15,15])

ax=fig.add_subplot(1,1,1,projection='polar')

t=np.linspace(0,1,1000)

for i in range(len(df)):

x_c=(1-t)*(1-t)*df.iloc[i]['Startx']+2*t*(1-t)*df.iloc[i]['控制点x']+t*t*df.iloc[i]['Endx']

y_c=(1-t)*(1-t)*df.iloc[i]['Starty']+2*t*(1-t)*df.iloc[i]['控制点y']+t*t*df.iloc[i]['Endy']

r=np.power(y_c*y_c+x_c*x_c,0.5)

theta=np.arctan2(y_c,x_c)

ax.plot(theta,r,color=(0/255,123/255,187/255),alpha=0.5)

ax.scatter(df[['theta1','theta2']],df[['r1','r2']],color=(0/255,123/255,87/255),alpha=1)

ax.bar(x=df['theta2'],height=[2],bottom=6,width=0.01,color=(225/255,225/255,225/255),alpha=0.5)

plt.axis('off')

讲解留到以后吧

之前一直以为参数算错了 后来发现是坐标轴的起点没有设置好

需改了坐标轴设置后,在增加对粗细的修改