天天看點

python繪制貝塞爾曲線_?用Python把圖做的好看點:用Matplotlib畫個Circos和弦圖

python繪制貝塞爾曲線_?用Python把圖做的好看點:用Matplotlib畫個Circos和弦圖

一直覺得circos圖 很好看,最近嘗試了一下 對中間關系曲線的 繪制

python繪制貝塞爾曲線_?用Python把圖做的好看點:用Matplotlib畫個Circos和弦圖

主要利用的數學工具是 貝塞爾曲線

但是對貝塞爾曲線中間的控制點取值 還是不太滿意 造成部分曲線看着有點奇怪

成品大概是這樣

python繪制貝塞爾曲線_?用Python把圖做的好看點:用Matplotlib畫個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')
           

講解留到以後吧

之前一直以為參數算錯了 後來發現是坐标軸的起點沒有設定好

需改了坐标軸設定後,在增加對粗細的修改

python繪制貝塞爾曲線_?用Python把圖做的好看點:用Matplotlib畫個Circos和弦圖