简体   繁体   English

绘制数据框的轮廓图,x 轴作为日期时间,y 轴作为深度

[英]Plotting Contour plot for a dataframe with x axis as datetime and y axis as depth

I have a dataframe with the indexes as datetime and columns as depths.我有一个数据框,索引为日期时间,列为深度。 I would like to plot a contour plot which looks something like the image below.我想绘制一个类似于下图的等高线图。 Any ideas how I should go about doing this?任何想法我应该怎么做? I tried using the plt.contour() function but I think I have to sort out the arrays for the data first.我尝试使用plt.contour()函数,但我想我必须先整理数据的数组。 I am unsure about this part.我不确定这部分。

Example of my dataframe:我的数据框示例:

Datetime             -1.62  -2.12  -2.62  -3.12  -3.62  -4.12  -4.62  -5.12                                                        
2019-05-24 15:45:00   4.61   5.67   4.86   3.91   3.35   3.07   3.03   2.84   
2019-05-24 15:50:00   3.76   4.82   4.13   3.32   2.84   2.40   2.18   1.89   
2019-05-24 15:55:00   3.07   3.77   3.23   2.82   2.41   2.21   1.93   1.81   
2019-05-24 16:00:00   2.50   2.95   2.63   2.29   1.97   1.73   1.57   1.48   
2019-05-24 16:05:00   2.94   3.62   3.23   2.82   2.62   2.31   2.01   1.81   
2019-05-24 16:10:00   3.07   3.77   3.23   2.82   2.51   2.31   2.10   1.89   
2019-05-24 16:15:00   2.71   3.20   2.86   2.70   2.51   2.31   2.18   1.97   
2019-05-24 16:20:00   2.50   3.07   2.86   2.82   2.73   2.50   2.37   2.22   
2019-05-24 16:25:00   2.40   3.20   3.10   2.93   2.73   2.50   2.57   2.84   
2019-05-24 16:30:00   2.21   2.95   2.86   2.70   2.73   2.72   2.91   3.49   
2019-05-24 16:35:00   2.04   2.72   2.63   2.59   2.62   2.72   3.03   3.35   
2019-05-24 16:40:00   1.73   2.31   2.33   2.39   2.62   2.95   3.57

Example of the plot I want:我想要的情节示例:

在此处输入图片说明

For the XYZ input in plt.contour() , I would like to find out what structure of data it requires.对于plt.contour()的 XYZ 输入,我想找出它需要什么数据结构。 It says it requires a 2D array structure, but I am confused.它说它需要一个二维数组结构,但我很困惑。 How do I get that with my current dataframe?我如何使用我当前的数据框获得它?

I have worked out a solution.我已经想出了一个解决方案。 Note that the X (tt2) - time input, and Y(depth) - depth input, have to match the Z(mat2) matrix dimensions for the plt.contourf to work.请注意,X (tt2) - 时间输入和 Y(depth) - 深度输入,必须与 Z(mat2) 矩阵维度匹配才能使 plt.contourf 工作。 I realised plt.contourf produces the image i want rather than plt.contour, which only plots the contour lines.我意识到 plt.contourf 生成我想要的图像而不是 plt.contour,它只绘制轮廓线。

Example of my code:我的代码示例:

tt2 = [...]
depth = [...]   
plt.title('SSC Contour Plot')
fig=plt.contourf(tt2,depth,mat2,cmap='jet',levels= 
[0,2,4,6,8,10,12,14,16,18,20,22,24,26], extend= "both")
plt.gca().invert_yaxis() #to flip the depth from shallowest to deepest
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M:%S'))
#plt.gca().xticklabels(tt)
cbar = plt.colorbar()
cbar.set_label("mg/l")
yy = len(colls2)
plt.ylim(yy-15,0) #assumming last 10 depth readings have NaN
plt.xlabel("Datetime")
plt.xticks(rotation=45)
plt.ylabel("Depth (m)")
plt.savefig(path+'SSC contour plot.png') #save plot 
plt.show()

Example of plot produced生成的情节示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python3 中绘制散点图 plot,其中 x 轴是纬度/经度,单位为千米,y 轴是深度 - Plotting a scatter plot in python3 where x axis is latitude/longitude in km and y axis is depth 在 x 和 y 轴上绘制符号 - plotting with symbols on x and y axis 如何指定x和y轴以在Python中绘制数据帧 - How to specify x and y axis for plotting dataframe in Python 绘制 DataFrame x,y,z(数据透视表)- 切换轴 - Plotting DataFrame x,y,z (Pivot-Table) - Switch axis 为(x,y,f(x,y))形式的轮廓图设置任意轴值? - Setting arbitrary axis value for a contour plot of form (x,y,f(x,y))? 绘制一个图,右侧有一个额外的 y 轴,顶部有一个额外的 x 轴,链接到底部 - Plotting a plot with an additional y axis on the right and an additional x axis on the top, linked to the bottom one 在 pandas dataframe plot 上拉伸 x 轴的部分(绘制不均匀的 x 轴) - stretch out portion of x-axis on pandas dataframe plot (plotting uneven x-axis) 如何更改等高线图中的x轴限制? - How to change the x axis limit in a Contour plot? 如何在Python中从数据框绘制带有x和y轴的直方图 - How to plot histogram with x and y axis from dataframe in python 绘制熊猫多索引DataFrame,其中一个索引作为Y轴,另一个作为X轴 - Plotting pandas multi-index DataFrame with one index as Y-axis and other as X-axis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM