Spectroscopic plot data from pandas data in 3D with different array lengths

Is it possible to get something like this plot

from a pandas frame, similar to how I would just do to make 2d graphics ( df.plot() )?

More precisely:

I have data that I read from csv files in pandas DataFrames with the following structure:

 1st level header ABCDEF 2nd level header 2.0 1.0 0.2 0.4 0.6 0.8 Index 126.4348 -467048 -814795 301388 298430 -187654 -1903170 126.4310 -468329 -810060 304366 305343 -192035 -1881625 126.4272 -469209 -804697 305795 312472 -197013 -1854848 126.4234 -469685 -799604 305647 318936 -200957 -1827665 126.4195 -469795 -795708 304101 323922 -202192 -1805153 126.4157 -469610 -793795 301497 326780 -199323 -1791743 126.4119 -469213 -794362 298257 327092 -191547 -1790418 126.4081 -468687 -797499 294817 324717 -178875 -1802122 126.4043 -468097 -802853 291546 319800 -162225 -1825540 126.4005 -467486 -809663 288700 312745 -143334 -1857270 126.3967 -466863 -816878 286401 304170 -124505 -1892389 126.3929 -466210 -823335 284645 294827 -108228 -1925312 126.3890 -465485 -827966 283331 285520 -96733 -1950795 126.3852 -464637 -829997 282315 277018 -91559 -1964894 126.3814 -463617 -829104 281457 269965 -93242 -1965702 126.3776 -462399 -825487 280670 264824 -101170 -1953728 126.3738 -460982 -819857 279942 261819 -113660 -1931820 126.3700 -459408 -813317 279344 260927 -128242 -1904669 126.3662 -457757 -807177 279009 261885 -142112 -1877955 126.3624 -456143 -802715 279090 264233 -152667 -1857303 126.3585 -454700 -800940 279722 267380 -158023 -1847241 126.3547 -453566 -802397 280969 270692 -157406 -1850358 126.3509 -452862 -807050 282792 273579 -151350 -1866803 126.3471 -452672 -814262 285033 275591 -141627 -1894249 126.3433 -453030 -822898 287426 276486 -130942 -1928303 126.3395 -453910 -831501 289627 276273 -122426 -1963297 126.3357 -455223 -838544 291266 275222 -119021 -1993312 126.3319 -456834 -842695 292004 273824 -122882 -2013246 126.3280 -458571 -843048 291599 272725 -134907 -2019718 126.3242 -460252 -839292 289952 272620 -154497 -2011656 ... ... ... ... ... ... ... 

What I would like to do with this

I would like to build each of these columns (they are NMR spectra) against the index. In a 2D overlay, this is a simple use of the pandas wrapper around matplotlib. However, I would like to build each spectrum in its “line” along the third axis, which has headings of the second level, like ticks. I tried using the functionality of matplotlib 3D graphics, but it seems to be suitable only if you actually have three arrays of the same length, which in the case of my data simply does not make sense, because each spectrum is recorded for one from the values ​​from the header of the second level.

Perhaps I get too complicated when I try to make a 3D plot?

I would like my plot to look like not a real 3D plot, but rather some special version of superimposed 2D graphics?

How i would like to do it

Bonus points for:

  • Using python only
  • Using only pandas and matplotlib
  • Functionality already implemented

If there is no obvious python way to do this, I would also welcome libraries of other languages ​​that can do the same, like R or Octave. I'm just not so familiar with them, so probably I will not be able to adapt more hacker solutions in these languages ​​according to my requirements.

This question may be very similar, but as I understand it, it does not necessarily apply to software other than python, and has no example that the result should look like this, so I'm not sure if the answers to this question may be useful for this specific purpose.

What is wrong with matplotlib gallery examples

As Laniri remarked, the polygon3D from the matplotlib gallery is getting closer to what I want. However, it has some drawbacks, some of which are unacceptable for most scientific publications:

  • With negative values, the whole plot goes over to what I would call the “middle of the screen”, which looks ugly, makes it difficult to extract information from the picture and make it different from the above examples
  • You get an interactive plot window in which you need to find the angle from which you can see everything you need to see. Which may be useful for some data mining tasks, but if you use scripts for your visualization and a slight change in the schedule forces you to do manual work again, this will reduce the advantage that you expect from scripts
  • If you have values ​​that are very different and not linear, something like [0,1,1.7,2.5,6.2] , for your third dimension, that is, the second level in this case, 2d graphs have very different distances from another, which is unacceptable, at least for any non-programming audience reading publications.
  • It is quite long and technical for a fairly ordinary build operation in spectroscopy. The amount of code would be nice if I wanted to create software that can create 3D graphics in a specific context. For science, it would be preferable to do something similar with a bit of code.
+5
source share
1 answer

I gave you an example of building with data from continuous X and Y and just hardcoded z based on your second level header.

 from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt import pandas as pd import matplotlib %matplotlib inline df = pd.read_csv("C:\Users\User\SkyDrive\Documents\import_data.tcsv.txt",header=None) fig = plt.figure() ax = fig.gca(projection='3d') # Plot a sin curve using the x and y axes. x = df[0] ax.plot(x, df[1], zs=2, zdir='z', label='A') ax.plot(x, df[2], zs=1, zdir='z', label='B') ax.plot(x, df[3], zs=0.2, zdir='z', label='C') ax.plot(x, df[4], zs=0.4, zdir='z', label='D') ax.plot(x, df[5], zs=0.6, zdir='z', label='E') ax.plot(x, df[6], zs=0.8, zdir='z', label='F') # Customize the view angle so it easier to see that the scatter points lie # on the plane y=0 ax.view_init(elev=-150., azim=40) plt.show() 

You will need to play with the parameters on view_init to rotate and get the axes wherever you want. I do not quite understand what your ultimate goal was, but this is the final plot.

enter image description here

0
source

All Articles