Question

I have data that I make a scatter plot for i.e. (x,y). But for each point I also have a third value say for example the temperature at that point. I'd like to make a colorbar next to the y axis which represents the temperature variation for my points. I have looked at documentation on the matplotlib website but can't seem to figure out. I have my data as lists so for instance:

x = [1,2,3,4]
y = [10,20,30,40]
and Temp = [100,200,300,400]

I make plots using plt.plot(x,y) but for the colorbars what goes in plt.colorbar(?)?

It's a really simple problem I am sure, but I need the solution fairly soon and am having trouble working it out so will appreciate any suggestion. Thanks.

Était-ce utile?

La solution

You can use plt.colorbar() straight up, without arguments, because pyplot knows about your scatter plot. As a previous commenter suggested, it's easier to use plt.scatter for this example:

import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [10,20,30,40]
Temp = [100,200,300,400]

plt.scatter(x,y,c=Temp)
plt.colorbar()
plt.show()

You can put all sorts of weird and wonderful arguments into plt.scatter. Check out the documentation and also the matplotlib thumbnails for inspiration. Happy matplotlibbing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top