plot a 3d plot using dataframe in matplotlib












0















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









share|improve this question

























  • 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.

    – ImportanceOfBeingErnest
    Nov 25 '18 at 16:38
















0















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









share|improve this question

























  • 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.

    – ImportanceOfBeingErnest
    Nov 25 '18 at 16:38














0












0








0








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









share|improve this question
















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






python pandas matplotlib plot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 16:40







Amir

















asked Nov 25 '18 at 16:34









AmirAmir

113




113













  • 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.

    – ImportanceOfBeingErnest
    Nov 25 '18 at 16:38



















  • 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.

    – ImportanceOfBeingErnest
    Nov 25 '18 at 16:38

















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.

– ImportanceOfBeingErnest
Nov 25 '18 at 16:38





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.

– ImportanceOfBeingErnest
Nov 25 '18 at 16:38












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer
























  • 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.

    – Amir
    Nov 25 '18 at 21:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469589%2fplot-a-3d-plot-using-dataframe-in-matplotlib%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer
























  • 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.

    – Amir
    Nov 25 '18 at 21:25
















0














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






share|improve this answer
























  • 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.

    – Amir
    Nov 25 '18 at 21:25














0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 17:11









ImportanceOfBeingErnestImportanceOfBeingErnest

137k13156235




137k13156235













  • 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.

    – Amir
    Nov 25 '18 at 21:25



















  • 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.

    – Amir
    Nov 25 '18 at 21:25

















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.

– Amir
Nov 25 '18 at 21:25





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.

– Amir
Nov 25 '18 at 21:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469589%2fplot-a-3d-plot-using-dataframe-in-matplotlib%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin