简体   繁体   中英

Plotting data from a data file in matplotlib

I have test data file with 16 rows and 3 columns and is something like:

4.0 4.0 0.992561656631 
7.33333333333 4.0 0.983625465545 
4.0 7.33333333333 0.983625465545 
7.33333333333 7.33333333333 0.973260418741 
10.6666666667 4.0 0.973385993787 
4.0 10.6666666667 0.973385993787 
10.6666666667 7.33333333333 0.96232158762 
7.33333333333 10.6666666667 0.96232158762 
10.6666666667 10.6666666667 0.947002325682 
14.0 4.0 0.963985902172 
4.0 14.0 0.963985902172 
14.0 7.33333333333 0.948250293872 
7.33333333333 14.0 0.948250293872 
14.0 10.6666666667 0.933855073978 
10.6666666667 14.0 0.933855073978 
14.0 14.0 0.91658870141

In fact, the first two columns show the coordinate and the 3rd column shows the values of a function in that coordinate. You can think of this table as a 4x4 matrix, if you wish.

Now, I want to plot this data file as a colorful 2D plot such that the first two columns show the coordinate and the 3rd column show the color of the box.

Following is part of my code that is supposed to take care of plotting:

    x3,y3,z3 = np.loadtxt("./data/FDFD_Real_Effectualness_m1m2_mo_%s_%s.dat" % (waveform2, waveform1)).T

    nrows, ncols = final_step_j-1, final_step_k-1
    grid3 = z3.reshape((nrows, ncols))

fig3 = plt.gcf()
            plt.xlabel('$m_1$')
            plt.ylabel('$m_2$')
            plt.imshow(grid3, extent=(x3.min(), x3.max(), y3.min(), y3.max()), origin='lower', aspect='auto', interpolation='nearest', cmap=cm.gist_rainbow)
            fig3.suptitle('Effectualness of %s and %s' % (waveform1, waveform2))
            plt.colorbar()
            plt.draw()
            fig3.savefig('./plots/FDFD_Real_Effectualness_m1m2_mo_%s_%s.pdf' %(waveform1, waveform2), dpi=100)
            plt.close()

The result does not look like what is expected (attached picture). In particular, from the data it is clear that the results should be diagonally symmetric. Any suggestion is welcome. 在此输入图像描述

You need to order your data before reshape it into a grid:

import pylab as pl
import io
import numpy as np

txt = """4.0 4.0 0.992561656631
7.33333333333 4.0 0.983625465545
4.0 7.33333333333 0.983625465545
7.33333333333 7.33333333333 0.973260418741
10.6666666667 4.0 0.973385993787
4.0 10.6666666667 0.973385993787
10.6666666667 7.33333333333 0.96232158762
7.33333333333 10.6666666667 0.96232158762
10.6666666667 10.6666666667 0.947002325682
14.0 4.0 0.963985902172
4.0 14.0 0.963985902172
14.0 7.33333333333 0.948250293872
7.33333333333 14.0 0.948250293872
14.0 10.6666666667 0.933855073978
10.6666666667 14.0 0.933855073978
14.0 14.0 0.91658870141"""

data = np.loadtxt(io.BytesIO(txt), delimiter=" ")
idx = np.lexsort((data[:, 0], data[:, 1]))
data = data[idx]
pl.imshow(data[:, 2].reshape(4, 4), origin='lower', interpolation='nearest')

Here is the output:

在此输入图像描述

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