9

I am using Python and a CSV file. I am currently trying to modify the scatter plot(2d) below to change colors based on a third column in my csv file. After searching through multiple posts, I basically want to use a generic colormap (rainbow) and multiply my third array by the colormap in order to display different colors for each of the xy points. I think I can do everything from the ax.scatter function but I am not sure how to multiply each different x,y coordinate by the colormap and the third array number. It should look similar to a contour plot, but I would prefer a different colored scatter plot.

Here is the code I am using:

import matplotlib   
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas    
from matplotlib.figure import Figure
import matplotlib.mlab as mlab
import numpy as np

r = mlab.csv2rec('test.csv')
fig = Figure(figsize=(6,6))
canvas = FigureCanvas(fig)

ax = fig.add_subplot(111)
ax.set_title("X vs Y AVG",fontsize=14)
ax.set_xlabel("XAVG",fontsize=12)
ax.set_ylabel("YAVG",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')

x = r.xavg #first column
y = r.yavg #second column
z = r.wtr #third column

ax.scatter(x,y,s=.2,c='b', marker = ',', cmap = ?);
Hooked
  • 84,485
  • 43
  • 192
  • 261
Jonny
  • 619
  • 4
  • 12
  • 31

1 Answers1

14

check out the scatter line

import matplotlib.pyplot as plt
from matplotlib  import cm
import numpy as np

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
ax.set_title("X vs Y AVG",fontsize=14)
ax.set_xlabel("XAVG",fontsize=12)
ax.set_ylabel("YAVG",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')
x = np.random.random(30)
y = np.random.random(30)
z = np.random.random(30)

# scatter with colormap mapping to z value
ax.scatter(x,y,s=20,c=z, marker = 'o', cmap = cm.jet );

plt.show()

and it produces

enter image description here

nye17
  • 12,857
  • 11
  • 58
  • 68
  • Thank you very much, that answered my question. I assume now that in order to change the color map, I can follow the other documentation. I also wanted to keep the canvas items in the code so that I could use the below code to save the image: 'canvas.print_figure('test.png', dpi=250)' – Jonny May 29 '12 at 13:20
  • @Jonny you don't necessarily need the canvas object to save your figures, you can use [savefig](http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig). – nye17 May 29 '12 at 17:05