日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python金字塔图绘制_如何用R或Python绘制3d(4变量)三元(金字塔)图?

發布時間:2025/3/15 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python金字塔图绘制_如何用R或Python绘制3d(4变量)三元(金字塔)图? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

嗯,我自己用了wikipedia article、SO post和一些蠻力解決了這個問題。對不起,代碼墻,但你必須畫出所有的繪圖輪廓和標簽等等。在import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import axes3d, Axes3D

from itertools import combinations

import pandas as pd

def plot_ax(): #plot tetrahedral outline

verts=[[0,0,0],

[1,0,0],

[0.5,np.sqrt(3)/2,0],

[0.5,0.28867513, 0.81649658]]

lines=combinations(verts,2)

for x in lines:

line=np.transpose(np.array(x))

ax.plot3D(line[0],line[1],line[2],c='0')

def label_points(): #create labels of each vertices of the simplex

a=(np.array([1,0,0,0])) # Barycentric coordinates of vertices (A or c1)

b=(np.array([0,1,0,0])) # Barycentric coordinates of vertices (B or c2)

c=(np.array([0,0,1,0])) # Barycentric coordinates of vertices (C or c3)

d=(np.array([0,0,0,1])) # Barycentric coordinates of vertices (D or c3)

labels=['a','b','c','d']

cartesian_points=get_cartesian_array_from_barycentric([a,b,c,d])

for point,label in zip(cartesian_points,labels):

if 'a' in label:

ax.text(point[0],point[1]-0.075,point[2], label, size=16)

elif 'b' in label:

ax.text(point[0]+0.02,point[1]-0.02,point[2], label, size=16)

else:

ax.text(point[0],point[1],point[2], label, size=16)

def get_cartesian_array_from_barycentric(b): #tranform from "barycentric" composition space to cartesian coordinates

verts=[[0,0,0],

[1,0,0],

[0.5,np.sqrt(3)/2,0],

[0.5,0.28867513, 0.81649658]]

#create transformation array vis https://en.wikipedia.org/wiki/Barycentric_coordinate_system

t = np.transpose(np.array(verts))

t_array=np.array([t.dot(x) for x in b]) #apply transform to all points

return t_array

def plot_3d_tern(df,c='1'): #use function "get_cartesian_array_from_barycentric" to plot the scatter points

#args are b=dataframe to plot and c=scatter point color

bary_arr=df.values

cartesian_points=get_cartesian_array_from_barycentric(bary_arr)

ax.scatter(cartesian_points[:,0],cartesian_points[:,1],cartesian_points[:,2],c=c)

#Create Dataset 1

np.random.seed(123)

c1=np.random.normal(8,2.5,20)

c2=np.random.normal(8,2.5,20)

c3=np.random.normal(8,2.5,20)

c4=[100-x for x in c1+c2+c3] #make sur ecomponents sum to 100

#df unecessary but that is the format of my real data

df1=pd.DataFrame(data=[c1,c2,c3,c4],index=['c1','c2','c3','c4']).T

df1=df1/100

#Create Dataset 2

np.random.seed(1234)

c1=np.random.normal(16,2.5,20)

c2=np.random.normal(16,2.5,20)

c3=np.random.normal(16,2.5,20)

c4=[100-x for x in c1+c2+c3]

df2=pd.DataFrame(data=[c1,c2,c3,c4],index=['c1','c2','c3','c4']).T

df2=df2/100

#Create Dataset 3

np.random.seed(12345)

c1=np.random.normal(25,2.5,20)

c2=np.random.normal(25,2.5,20)

c3=np.random.normal(25,2.5,20)

c4=[100-x for x in c1+c2+c3]

df3=pd.DataFrame(data=[c1,c2,c3,c4],index=['c1','c2','c3','c4']).T

df3=df3/100

fig = plt.figure()

ax = Axes3D(fig) #Create a 3D plot in most recent version of matplot

plot_ax() #call function to draw tetrahedral outline

label_points() #label the vertices

plot_3d_tern(df1,'b') #call function to plot df1

plot_3d_tern(df2,'r') #...plot df2

plot_3d_tern(df3,'g') #...

總結

以上是生活随笔為你收集整理的python金字塔图绘制_如何用R或Python绘制3d(4变量)三元(金字塔)图?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。