2

I have following data and i am having trouble plotting a 3d Plot similar to the one showed in the examples of Matplotlib -> https://matplotlib.org/examples/mplot3d/custom_shaded_3d_surface.html

On the x axis i want to have the Residue column, on the y-axis the first row and the z axis should represent the values.

     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

Is it better not to use a Dataframe in this case?

this is the code im using:

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()
1
  • You can sure use a dataframe. One problem is that residue is a column of the frame, so it would be included in the data. however, you would rather make it the dateframe index. Then you will need to create a meshgrid of the index and columns as shown in the many examples on this topic. Commented Nov 25, 2018 at 16:38

1 Answer 1

2
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")

Setting such that the residue column is not part of the data anymore.

enter image description here

Then you can create the meshgrid from the columns and the index and plot it according to the linked example.

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()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much that was exactly what i was looking for, the df.value. I actually added the column name 8residue, 1 ,2,3, etc. manually but apperently it wasnt a smart strategy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.