欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python画3D图

程序员文章站 2022-07-13 09:13:41
...
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

df=pd.DataFrame({'X': range(1,101), 'Y': np.random.randn(100)*15+range(1,101), 'Z': (np.random.randn(100)*15+range(1,101))*2 })
 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(df['X'], df['Y'], df['Z'], c='skyblue', s=60)
ax.view_init(30, 185)
plt.show()

Python画3D图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

url = 'https://python-graph-gallery.com/wp-content/uploads/volcano.csv'
data = pd.read_csv(url)

df=data.unstack().reset_index() 
df.columns=["X","Y","Z"]

df['X']=pd.Categorical(df['X'])
df['X']=df['X'].cat.codes

fig = plt.figure()
ax = fig.gca(projection='3d')
surf=ax.plot_trisurf(df['Y'], df['X'], df['Z'], cmap=plt.cm.viridis, linewidth=0.2)
fig.colorbar( surf, shrink=0.5, aspect=5)
plt.show()

 Python画3D图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

url = 'https://python-graph-gallery.com/wp-content/uploads/volcano.csv'
data = pd.read_csv(url)

df=data.unstack().reset_index() 
df.columns=["X","Y","Z"]

df['X']=pd.Categorical(df['X'])
df['X']=df['X'].cat.codes

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(df['Y'], df['X'], df['Z'], cmap=plt.cm.jet, linewidth=0.01)
plt.show()

 Python画3D图

 

import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

import seaborn as sns
sns.set_style("white")
df = sns.load_dataset('iris')

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

df['species']=pd.Categorical(df['species'])
my_color=df['species'].cat.codes
df = df.drop('species', 1)

pca = PCA(n_components=3)
pca.fit(df)

result=pd.DataFrame(pca.transform(df), columns=['PCA%i' % i for i in range(3)], index=df.index)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(result['PCA0'], result['PCA1'], result['PCA2'], c=my_color, cmap="Set2_r", s=60)

xAxisLine = ((min(result['PCA0']), max(result['PCA0'])), (0, 0), (0,0))     
ax.plot(xAxisLine[0], xAxisLine[1], xAxisLine[2], 'r') 
yAxisLine = ((0, 0), (min(result['PCA1']), max(result['PCA1'])), (0,0)) 
ax.plot(yAxisLine[0], yAxisLine[1], yAxisLine[2], 'r') 
zAxisLine = ((0, 0), (0,0), (min(result['PCA2']), max(result['PCA2']))) 
ax.plot(zAxisLine[0], zAxisLine[1], zAxisLine[2], 'r') 
 
ax.set_xlabel("PC1") 
ax.set_ylabel("PC2")
ax.set_zlabel("PC3")
ax.set_title("PCA on the iris data set")
plt.show()

Python画3D图

import matplotlib.pyplot as plt
import pandas as pd
from math import pi

df = pd.DataFrame({
    'group': ['A','B','C','D'],
    'var1': [38, 1.5, 30, 4], 
    'var2': [29, 10, 9, 34], 
    'var3': [8, 39, 23, 24], 
    'var4': [7, 31, 33, 14], 
    'var5': [28, 15, 32, 14]
    })

def make_spider( row, title, color):
    
    categories=list(df)[1:]
    N = len(categories)

    angles = [n / float(N) * 2 * pi for n in range(N)]
    angles += angles[:1]

    ax = plt.subplot(2,2,row+1, polar=True, )

    ax.set_theta_offset(pi / 2)
    ax.set_theta_direction(-1)

    plt.xticks(angles[:-1], categories, color='grey', size=8)

    ax.set_rlabel_position(0)
    plt.yticks([10,20,30], ["10","20","30"], color="grey", size=7)
    plt.ylim(0,40)

    values=df.loc[row].drop('group').values.flatten().tolist()
    values += values[:1]
    ax.plot(angles, values, color=color, linewidth=2, linestyle='solid')
    ax.fill(angles, values, color=color, alpha=0.4)

    # 添加标题
    plt.title(title, size=11, color=color, y=1.1)


my_dpi=96
plt.figure(figsize=(1000/my_dpi, 1000/my_dpi), dpi=my_dpi)

my_palette = plt.cm.get_cmap("Set2", len(df.index))

for row in range(0, len(df.index)):    
    make_spider( row=row, title='group '+df['group'][row], color=my_palette(row))
plt.show()

 Python画3D图

本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。

Python画3D图 

本文来自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py