[英]plot a 3d plot using dataframe in matplotlib
我有以下数据,并且在绘制类似于Matplotlib示例-> https://matplotlib.org/examples/mplot3d/custom_shaded_3d_surface.html的示例中显示的3d图时遇到了麻烦。
在x轴上,我要有“残基”列,在y轴上,第一行和z轴应代表值。
residue 0 1 2 3 4 5 6 \
0 0.0 0.0 1.671928 1.441439 0.808492 1.079337 1.186970 1.445275
1 1.0 0.0 1.348867 1.216174 1.324360 1.965453 2.121130 1.713321
2 2.0 0.0 1.281589 0.794236 1.083470 1.476939 2.011159 2.360246
3 3.0 0.0 0.798151 0.993858 1.020617 0.829792 1.280412 1.653299
4 4.0 0.0 0.789995 1.194215 1.407934 1.291384 1.555449 1.258266
5 5.0 0.0 0.653958 0.910582 1.585495 1.245847 1.620384 1.664490
6 6.0 0.0 0.782577 0.648373 1.284292 1.087762 1.523729 1.631152
7 7.0 0.0 1.094054 1.127248 0.958693 1.168483 0.897470 1.404080
8 8.0 0.0 0.433993 1.165169 0.925521 1.292363 1.075700 1.146139
9 9.0 0.0 1.114398 0.963963 1.062597 1.297358 1.412016 1.422071
10 10.0 0.0 0.706276 1.056272 1.381639 1.682080 1.779487 1.914487
11 11.0 0.0 1.059623 1.000653 1.152697 1.895022 1.562730 1.964862
在这种情况下最好不要使用数据框吗?
这是我使用的代码:
z = df.iloc[1:,1:-1]
ff= [i for i in range(1,500)]
y=df["residue"]
print(len(z))
nrows, ncols = z.shape
x = np.linspace(min(ff),max(ff), ncols)
x, y = np.meshgrid(x, y)
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
plt.show()
u = """ residue 0 1 2 3 4 5 6
0 0.0 0.0 1.671928 1.441439 0.808492 1.079337 1.186970 1.445275
1 1.0 0.0 1.348867 1.216174 1.324360 1.965453 2.121130 1.713321
2 2.0 0.0 1.281589 0.794236 1.083470 1.476939 2.011159 2.360246
3 3.0 0.0 0.798151 0.993858 1.020617 0.829792 1.280412 1.653299
4 4.0 0.0 0.789995 1.194215 1.407934 1.291384 1.555449 1.258266
5 5.0 0.0 0.653958 0.910582 1.585495 1.245847 1.620384 1.664490
6 6.0 0.0 0.782577 0.648373 1.284292 1.087762 1.523729 1.631152
7 7.0 0.0 1.094054 1.127248 0.958693 1.168483 0.897470 1.404080
8 8.0 0.0 0.433993 1.165169 0.925521 1.292363 1.075700 1.146139
9 9.0 0.0 1.114398 0.963963 1.062597 1.297358 1.412016 1.422071
10 10.0 0.0 0.706276 1.056272 1.381639 1.682080 1.779487 1.914487
11 11.0 0.0 1.059623 1.000653 1.152697 1.895022 1.562730 1.964862"""
import io
import pandas as pd
import numpy as np
df = pd.read_csv(io.StringIO(u), delim_whitespace=True)
df = df.set_index("residue")
设置为使得residue
列不再是数据的一部分。
然后,您可以根据列和索引创建网格网格,并根据链接的示例对其进行绘制。
x,y = np.meshgrid(df.columns.astype(float), df.index)
z = df.values
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LightSource
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
rgb = LightSource(270, 45).shade(z, cmap=plt.cm.gist_earth, vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(x, y, z, facecolors=rgb,
linewidth=0, antialiased=False, shade=False)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.