Python学习 —— matplotlib绘制三维曲线图和三维散点图

1.数据类型部分如下:

group	A	B	C
A-1	0	0	0.1273 
A-1	20	12	0.1087 
A-1	40	36	0.1313 
A-1	60	24	0.0865 
A-1	80	12	0.0980 
A-1	100	0	0.0860 
A-1	120	12	0.0520 
A-1	140	36	0.0475 
B-1	0	0	0.1293 
B-1	20	12	0.1097 
B-1	40	36	0.1223 
B-1	60	24	0.0765 
B-1	80	12	0.0190 
B-1	100	0	0.0890 
B-1	120	12	0.0510 
B-1	140	36	0.0575 
C-1	0	0	0.1293 
C-1	20	12	0.1067 
C-1	40	36	0.1223 
C-1	60	24	0.0865 
C-1	80	12	0.0970 
C-1	100	0	0.0760 
C-1	120	12	0.0510 
C-1	140	36	0.0575 
D-1	0	0	0.1283 
D-1	20	12	0.1197 
D-1	40	36	0.1313 
D-1	60	24	0.0765 
D-1	80	12	0.0880 
D-1	100	0	0.0860 
D-1	120	12	0.0520 
D-1	140	36	0.0465 

三维图像之散点图代码如下:

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

file = 'C:\\Users\Desktop\huatu.txt'
data = pd.read_csv(file, sep="\t")
#print(data)
def sample_x():   #取出x轴数据
    df = []
    sample_type1 = data.group.unique()
    for i in sample_type1:
        a=data.concentration[data.group == i]
        df.append(a)
    return df
x1,x2,x3,x4 = sample_x()[0],sample_x()[1],sample_x()[2],sample_x()[3]
 
def sample_y():   #取出y轴数据
    df1 = []
    sample_type1 = data.group.unique()
    for i in sample_type1:
        b=data.time[data.group == i]
        df1.append(b)
    return df1
y1,y2,y3,y4 = sample_y()[0],sample_y()[1],sample_y()[2],sample_y()[3]

def sample_z():   #取出z轴数据
    df2 = []
    sample_type1 = data.group.unique()
    for i in sample_type1:
        c=data.OD600[data.group == i]
        df2.append(c)
    return df2
z1,z2,z3,z4 = sample_z()[0],sample_z()[1],sample_z()[2],sample_z()[3]

# 绘制散点图
fig = plt.figure()
ax = Axes3D(fig)

ax.scatter(x1, y1, z1, c='r', label='A')
ax.scatter(x2, y2, z2, c='g', label='B')
ax.scatter(x3, y3, z3, c='y', label='C')
ax.scatter(x4, y4, z4, c='b', label='D')

# 绘制图例
ax.legend(loc='best')

# 添加坐标轴(顺序是Z, Y, X)
ax.set_zlabel('Z', fontdict={'size': 15, 'color': 'red'})
ax.set_ylabel('Y', fontdict={'size': 15, 'color': 'red'})
ax.set_xlabel('X', fontdict={'size': 15, 'color': 'red'})

plt.show()

三维图像之曲线图代码如下:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import pandas as pd

file = 'C:\\Users\Desktop\huatu.txt'
data = pd.read_csv(file, sep="\t")
#print(data)
def sample_x():   #取出x轴数据
    df = []
    sample_type1 = data.group.unique()
    for i in sample_type1:
        a=data.concentration[data.group == i]
        df.append(a)
    return df
x1,x2,x3,x4 = sample_x()[0],sample_x()[1],sample_x()[2],sample_x()[3]

def sample_y():   #取出y轴数据
    df1 = []
    sample_type1 = data.group.unique()
    for i in sample_type1:
        b=data.time[data.group == i]
        df1.append(b)
    return df1
y1,y2,y3,y4 = sample_y()[0],sample_y()[1],sample_y()[2],sample_y()[3]

def sample_z():   #取出z轴数据
    df2 = []
    sample_type1 = data.group.unique()
    for i in sample_type1:
        c=data.OD600[data.group == i]
        df2.append(c)
    return df2
z1,z2,z3,z4 = sample_z()[0],sample_z()[1],sample_z()[2],sample_z()[3]

# new a figure and set it into 3d
fig = plt.figure()
ax = fig.gca(projection='3d')

# set figure information
ax.set_title("3D")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

# draw the figure, the color is r = read
figure1 = ax.plot(x1, y1, z1, c='r')
figure2 = ax.plot(x2, y2, z2, c='b')
figure3 = ax.plot(x3, x3, z3, c='g')
figure4 = ax.plot(x4, x4, z4, c='y')

plt.show()

 

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

 

 

  • 20
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值