Search code examples
pythonmatplotlibplotplotlycolorbrewer

Colour Map on scatter plot


I have three columns in a csv file which reads like this:

X,Y, Cosine efficiency
989.565,670.17,0.868684456
660.941,-1502.48,0.597998523
685.683,-1491.35,0.600137189
-1454.88,-316.582,0.666894492
-510.86,1016.79,0.870398677
-1350.21,-788.027,0.61601682
-944.034,-1342.79,0.565497692
-966.098,-1327,0.56678693
-1206.4,-1054.63,0.590095562
1004.18,648.067,0.866589585

I can read the X and Y values and make a scatter plot in the following way:

import csv
# Reading data from csv file
with open('my_file.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
    X = []
    Y = []
    C = []

for row in readCSV:
    X_coordinates = row[0]
    Y_coordinates = row[1]
    cos_eff = row[2]
    X.append(X_coordinates)
    Y.append(Y_coordinates)
    C.append(cos_eff)
Xcoordinate = [float(X[c]) for c in range(1,len(X))]
Ycoordinate=[float(Y[c]) for c in range(1,len(Y))]
CosineEff=[float(C[c]) for c in range(1,len(C))]

# Plotting the solar field
plt.figure(1)
plt.scatter(Xcoordinate,Ycoordinate, color = 'blue', s=2)
plt.title("Solar field")
plt.xlabel("Position, east-west [m]"); plt.ylabel("Position, east-west [m]")
plt.show()

Now, I want to make this scatter plot using a colour map which uses the values from the third column.

For example, the first point is : 989.565,670.17,0.868684456. I have already plotted the X and Y coordinates - in this case 989.565 and 670.17. But i want to use a colour map to put the value of 0.868684456 on this plotted X & Y value.

I want to use Brewer2mpl or pcolour but I dont know where to start.

I can make a colour map on Origin using the 'colour mapped' function but how can I do it on Python?


Solution

  • plt.scatter(x_vals, y_vals,  c = z_vals, cmap = 'rainbow')
    

    You're going to want to play with adding s= and choosing an appropriate size for the points. There's also a beautiful colormap set called cmocean that you may want to consider installing