简体   繁体   中英

Trouble plotting 2d contourf and 3d data using pd.DataFrames python

I am currently trying to produce a data viz of my experiment I did which involved xy scan files and then plotting them as a function of time. I have managed to get them into a pandas DataFrame that is (1027,281) in shape with the x axis as the index, time as the column labels and the values for the scan as the values in the df. The contour plot looks like this.

y = dftest.index.values
x = dftest.columns.values
z = dftest.values
X,Y = np.meshgrid(x,y)
z2 = np.ma.array(z)
z2_masked = np.ma.masked_where(z2 > 300, z2)
z2_masked = np.ma.masked_where(z2_masked < -5, z2_masked)
z3 = np.ma.filled(z2_masked, fill_value = 0)
plt.contourf(X, Y ,z3, 20, cmap = 'jet')
plt.colorbar()
plt.xlim(xmax = 175000)
plt.xticks(np.arange(0, 175000, step=50000))

Time resolved PXRD, x axis = time, y = diffraction angle:

在此处输入图片说明

Firstly that produces something that is the wrong way round, I would like the x value of the scan files to be the x axis, but instead it is the y axis. Then I would like to find a simple way of plotting this as a 3d contour map or surface. I think my problems lie in the shape of the data but I am not really sure how to correct it.

each of my DataFrame's looks like this:

           0.0       646.0       ...      181742.0   182390.0
x                                ...                         
0.996522   7.301625  3.914700    ...      8.224773   9.885618
1.000432  10.722788  7.379380    ...      8.474020  19.229299
1.004341   0.079724  5.567879    ...     -0.143427   2.684953
1.008251   4.738650  3.903460    ...     -1.162278   3.809588
1.012161   6.213206 -0.318955    ...      4.050190   1.454264
            ...       ...    ...           ...        ...
4.992126  -2.956039 -4.475446    ...     -2.816053  -4.556231
4.996036  -1.105434  1.274342    ...     -1.393612  -4.338330
4.999945  -0.536215  2.073975    ...     -2.727332  -1.083154
5.003855   5.983973  6.983155    ...      1.188320   3.657221
5.007765  -3.638785 -1.548692    ...     -5.225328  -2.164280

[1027 rows x 281 columns]

To generate your contour plot (ignoring the masking for simplicity), doesn't this work?

x = dftest.index.values
y = dftest.columns.values
z = dftest.values
X,Y = np.meshgrid(x,y)
Z = z.T
plt.contourf(X,Y,Z,20,cmap='jet')
plt.colorbar()
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM