0

I'm pretty new to matplotlib and I wanted to create as an output a plot and a scale ranging from 0-1; with the marker in the same color as the scale. I did it in a scatter graph with colorbar method but had to specify another imshow to draw the colorbar. The trouble is I get a rectangle from 0 to 0.5 the same color of the marker and after days trying I couldn't manage to errase it (without errasing the colorbar/scale).

Any help will be highly apreciated!

import matplotlib.pyplot as plt
import matplotlib.cm as cm

data =[0,0.7]
for l in data:         
    cmap1 = cm.RdYlBu_r(l)
    fig = plt.figure(dpi=300)
    ax = fig.add_subplot(1,1,1)
    ax.grid()

    sc = plt.scatter(0, l, s=500, color= cmap1)
    cax = ax.imshow([[l]],vmin=0, vmax=1, interpolation='nearest',aspect=5, cmap= plt.cm.RdYlBu_r)

    plt.yticks([0,0.5, 1])
    plt.xticks([])

    plt.colorbar(cax)
    axes = plt.gca()
    plt.show

This is the produced image:
returning image

Edit: I had already searched for 'matplotlib colorbar for scatter' and all variations but had an error when applying it such as 'TypeError: You must first set_array for mappable'. I guess due to the fact I only have one value to map at a time. Sorry if it is not like that; I am new at matplotlib and I understand I want to do soemthing a bit particular.

  • 1
    Can you specify how the resulting plot should look? You have the two datapoints `[0, 0.7]`. Do you want a plot for each datapoint and for each plot a colorbar indicating the same as the one marker? – elcombato Apr 25 '17 at 12:41
  • 1
    Possible duplicate of [matplotlib colorbar for scatter](http://stackoverflow.com/questions/6063876/matplotlib-colorbar-for-scatter) – Tom de Geus Apr 25 '17 at 12:54
  • Actually my idea was to have one plot with its colorbar for each datapoint. Somewhat the image that is generated but without the orange-coloured rectangle (it changes according to the datapoint). – Valentin Iglesias Apr 26 '17 at 10:14

1 Answers1

1

The colored rectangle you get comes from the imshow plot. Since you don't want that rectangle, remove the imshow plot.

Next step would be to get the colorbar. plt.colorbar expects a ScalarMappable as first argument. So a good idea would be to supply the scatter plot as argument, plt.colorbar(sc).

At this point you will probably get an error saying "You must first set_array for mappable". Looking closer at the scatter plot (plt.scatter(0, l, s=500, color= cmap1), once can see that there is indeed no mapping applied, since the single color is defined by the color argument. In order to become a useful ScalarMappable, you need to define an array which can be mapped to a color using the c argument. So instead of doing the mapping manually as you try to do with cmap1 = cm.RdYlBu_r(l), you should leave that step to the scatter plot.

sc = plt.scatter(0, l, s=500, c=l, vmin=0, vmax=1,cmap= plt.cm.RdYlBu_r)

Now the plot works, but looks bad, so you may want to apply some settings like aspect, limits figure size etc.

In total, you might end up with something like this:

import matplotlib.pyplot as plt

data =[0,0.4,0.7,0.9]
for l in data:         
    fig = plt.figure(figsize=(2,3), dpi=100)
    ax = fig.add_subplot(1,1,1, aspect=10)
    ax.grid()

    sc = plt.scatter(0, l, s=500, c=l, vmin=0, vmax=1,cmap= plt.cm.RdYlBu_r)

    plt.xlim([-1,1])
    plt.ylim([0,1])
    plt.yticks([0,0.5, 1])
    plt.xticks([])

    plt.colorbar(sc)
    plt.subplots_adjust(right=0.7)
    plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712