以不同的阵列长度绘制3D pandas 数据帧中的光谱数据 [英] Plot spectroscopic data from pandas dataframe in 3D with different array length
问题描述
是否可以获得类似以类似于我只是做2d绘图(df.plot()
)的方式从熊猫数据框中获取?
from a pandas dataframe, in a a similar fashion as I would just simply do to do 2d-plots (df.plot()
)?
我有从csv文件读取到具有以下结构的pandas DataFrames中的数据:
I have data that I read from csv files into pandas DataFrames with following structure:
1st level header A B C D E F
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
... ... ... ... ... ... ...
我想怎么做
我想针对这些指数绘制每个色谱柱(它们是NMR光谱). 在2D叠加层中,这是matplotlib周围的熊猫包装的简单用法. 但是,我想沿着具有第二级标题的第三轴在其自己的线"中绘制每个频谱. 滴答声. 我尝试使用matplotlib的3D绘图功能,但似乎只有在您实际上有三个长度相等的数组时才合适, 就我的数据而言,这是没有意义的,因为记录了每个光谱的第二级标题中的一个值.
What I would like to do with that
I would like to plot each of these columns (they are NMR spectra) against the index. In a 2D overlay, this is simple usage of the pandas wrapper around matplotlib. However, I would like to plot each spectrum in its own "line", along a third axis that has the second level headers as ticks. I tried to use matplotlib´s 3D plotting functionality, but it seems to only be suitable if you actually have three arrays of equal length, which in the case of my data does just not make sense, because each spectrum is recorded for one of the values from the second level header.
当我尝试制作3D图时,我可能会觉得太复杂了吗?
Am I maybe thinking too complicated when I try to make a 3D plot?
我想让我的绘图看起来像不是真实的3D绘图,而是某些特殊版本的2D绘图吗?
Is the figure I would like my plot to look like maybe not an actual 3D plot but rather some special version of overlaid 2D plots?
奖励积分:
- 仅使用python
- 仅使用熊猫和matplotlib
- 已实现的功能
如果没有明显的python方法可以做到,那么我也很高兴能够使用其他语言的库,例如 R或八度.我只是不熟悉这些内容,所以我可能无法适应这些语言的更多骇客解决方案,以满足我的要求.
If there is no obvious python way to do it, I would as well be happy about libraries of other languages that can do the same, such as R or Octave. I am just not as familiar with these, so I would probably not be able to adapt more hacky solutions in these languages to suit my requirements.
这个问题可能非常相似,但据我了解它,它不一定扩展到python以外的其他软件,也没有结果示例,因此我不确定该问题的答案是否可能实际上对此特定目的有所帮助.
This question might be very similar, but as I understand it, it does not necessarily extend to software other than python and doesn't have an example of what the result should look like, so I am not sure if answers to that question might actually be helpful for this specific purpose.
正如Lanery指出的那样,matplotlib画廊中的polygon3D接近了我的期望. 但是它有一些缺点,其中一些缺点对于大多数科学出版物都是不可接受的:
As lanery pointed out, polygon3D from the matplotlib gallery gets close to what I wish for. However it has some drawbacks some of which are not acceptable for most scientific publications:
- 值为负值时,整个图将移至我想要的位置 叫屏幕中间",看起来有点丑陋, 很难从图中提取信息并使其与众不同 从提供的示例中
- 您将获得一个交互式绘图窗口,需要您找到一个 您可以从中看到需要看到的一切的角度.那 可能对某些数据探索任务很有用,但是如果您使用 用于可视化的脚本和图形的较小更改 会迫使您再次做一些手动工作,这会减少 您期望从脚本编写中受益的优势
- 如果您的值相差很大并且不是线性的,则说明
像
[0,1,1.7,2.5,6.2]
一样,用于您的第三维,即第二维 在这种情况下,水平标头中的2d图具有非常不同的距离 至少对于任何其他人来说都是不可接受的 非编程观众阅读出版物 - 进行相当常见的绘图操作需要很长的时间和技术 在光谱学中.如果我想的话,代码量就可以了 构建可以在某些情况下绘制3D图的软件.为了科学 能够完成这样的事情将是更可取的 代码量少.
- With negative values, the whole plot gets shifted to what I would call "the middle of the screen", which looks kind of ugly, makes it hard to extract information from the figure and makes it different from the provided examples
- You get that interactive plot window, which requires you to find an angle from which you can see everything you need to see. That might be good for some data exploration tasks, but if you use scripts for your visualization and a minor change to the graphic would force you to do some manual work again, this decreases the advantage you expect from scripting
- If you have values that differ strongly and are not linear, something
like
[0,1,1.7,2.5,6.2]
, for your third dimension i.e. the second level header in this case, the 2d plots have very different distances from another, which is unacceptable, at least for any non-programming audience reading the publications - It is quite long and technical for a quite common plotting operation in spectroscopy. The amount of code would be fine if I wanted to build software that can make 3D plots in some context. For science it would be preferable to be able to accomplish something like this with a low amount of code.
推荐答案
我为您提供了一个示例,该示例使用来自连续X和Y的数据进行绘图,并仅基于第二级标头对z进行硬编码.
I gave you an example of plotting with the data from the continuous X and Y, and just hard-coded 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's easier to see that the scatter points lie
# on the plane y=0
ax.view_init(elev=-150., azim=40)
plt.show()
您将不得不使用view_init上的选项来旋转并获得所需的轴.我不清楚您的最终目标是什么,但这是最终的阴谋.
Your going to have to play with the options on view_init to rotate around and get the axes where you want. I'm not really clear with what your end goal was, but this is the end plot.
这篇关于以不同的阵列长度绘制3D pandas 数据帧中的光谱数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!