Python:具有固定x和y的2d等高线图,用于6个系列的分数数据(z)

时间:2016-10-14 15:00:35

标签: python python-2.7 pandas matplotlib

我尝试使用等高线图在6个高度(5,10,15,20,25和30)显示一个固定x轴的分数数据(0到1之间) (" WN"系列,1到2300)。 y(高度)对于每个系列是不同的并且是不连续的,因此我需要在高度之间进行插值。

WN,5,10,15,20,25,30
1,0.9984898,0.99698234,0.99547797,0.99397725,0.99247956,0.99098486
2,0.99814528,0.99629492,0.9944489,0.99260795,0.99077147,0.98893934
3,0.99765164,0.99530965,0.99297464,0.99064702,0.98832631,0.98601222
4,0.99705136,0.99411237,0.99118394,0.98826683,0.98535997,0.9824633
5,0.99606526,0.99214685,0.98824716,0.98436642,0.98050326,0.97665751
6,0.98111153,0.96281821,0.94508928,0.92790776,0.91125059,0.89509743
7,0.99266499,0.98539108,0.97816986,0.97100824,0.96390355,0.95685524
...

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

使用matplotlib,您需要X(行),Y(列)和Z值。 matplotlib函数需要特定格式的数据。下面,您将看到meshgrid帮助我们获得该格式。

在这里,我使用pandas导入我保存到csv文件的数据。您可以按照自己喜欢的方式加载数据。关键是为绘图功能准备数据。

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

#import the data from a csv file
data = pd.read_csv('C:/book1.csv')

#here, I let the x values be the column headers (could switch if you'd like)
#[1:] don't want the 'WN' as a value
X = data.columns.values[1:]

#Here I get the index values (a pandas dataframe thing) as the Y values
Y = data['WN']

#don't want this column in your data though
del data['WN']

#need to shape your data in preparation for plotting
X, Y = np.meshgrid(X, Y)

#see http://matplotlib.org/examples/pylab_examples/contour_demo.html
plt.contourf(X,Y,data)

enter image description here