简体   繁体   中英

In python - How can I plot 2D figure (x,y) and add 3rd axis by variation of the colour for each (x,y)-coordinate?

I want to add a 3rd axis data by adding a colour for each point on my 2D-plot.

I have measured the electric potential in an area, the X,Y is the lattitude and the longtitude. so each point on the 2D plot is a coordinate. Now I want to make each point a colour corresponding to the electrical potential measured (the z data). If the values are near each other they should be almost the same colour and a different if they are far apart.

I plotted it as 2D like this:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as pl

pl.plot(x,y, '*')
pl.xlabel('Latitude')
pl.ylabel('Longitude')

二维坐标图

And then I tried to make a contourplot, with the data from the z-axis, but the plot turns out weird. If I try to plot the z-data directly, it gives an error saying "TypeError: Input z must be a 2D array.".

x = lat
y = lon
z = EP

X, Y = np.meshgrid(x, y)
print (X.shape, Y.shape)
# (2, 7) (2, 7) Both have same shape
#Z = z.reshape(X.shape) # Use either X or Y to define shape
Z=np.tile(z,(211,1))
print(Z.shape)
#print(z)

fig = plt.figure()
ax1 = plt.contour(X,Y,Z)
plt.colorbar(ax1)
plt.show()

等高线图失败

I would like the plot to look a bit like this, where the dots have different colours depending on their value. Our x,y and z arrays are all arrays of one column and 211 rows, so they have the same size. 等高线图

You could try using pl.scatter

%matplotlib inline
import numpy as np
import matplotlib.pyplot as pl


graph = pl.scatter(x, y, c=z)

pl.xlabel('Latitude')
pl.ylabel('Longitude')
pl.colorbar(graph)

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