[matplotlib]colorbar scale problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gooddaytoday
    New Member
    • Jul 2010
    • 2

    [matplotlib]colorbar scale problem

    Hi,
    I am right now using matplotlib to plot some of my simulation results into contour graphs. My problems is:
    I have several simulation run results (three sets of results from three simulation runs). I plotted the results into contours graphs. Now I want to have the same colorbar scale for all three contours. For example, the first graph data ranges from 0 to 3, second from 0 to 5, and third is 0 to 9. I want to fix the colorbar for each graph from 0 to 9 (instead of different scales for each graph as is the default), and change the contour plot accordingly. How can I do that? Many many thanks!!!
    Here is the part of my code for plotting:
    Code:
    fig = plt.figure(figsize=(15, 5))
    fig.subplots_adjust(top=0.85)
    fig.add_subplot(121,title='Packet loss') 
    CS1 = plt.contourf(topo,100, cmap=my_cm) 
    # topo is an array with data inside
    CS1.set_clim(CS1.cvalues[0], CS1.cvalues[-2])
    CB1 = plt.colorbar(CS1)
    CB1.ax.set_ylabel('[%]')
    plt.yticks(range(sqr_node))
    plt.xticks(range(sqr_node))
    plt.show()
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Hi

    I've never got into color maps, although I've always been keen to. I'm a big fan of matplotlib. I'd imagine your question is quite common, so it would be great to let us know how it goes.

    I think that you can do it via the norm kwarg. It takes a colors.normaliz e object, which by default scales from 0 to 1 using the minimum and maximum of the given dataset. I suspect that if you create a normalise object with the min and max set according to your data you should be good to go.

    Sorry, I haven't had a chance to try it out, but please post back whether or not this helps.

    G

    Comment

    • gooddaytoday
      New Member
      • Jul 2010
      • 2

      #3
      Thanks Glenton.
      I just thought it over today, and it turned out to be pretty simple - just don't plot the colorbar according to the contour. Instead, I add a new axis for the color bar, and set the norm kwarg to the limits I wanted for colorbar.Colorb arBase. Also need to set the norm in plt.contourf as well. Then it is done.
      Here is my code:
      Code:
      # Contour
      norm1 = col.Normalize(vmin=0,vmax=9)
      ax1 = fig.add_axes([0.05,0.09,0.375,0.75], 
                          title='Packet loss', 
                          xticks=range(sqr_node),
                          yticks=range(sqr_node))
      CS1 = plt.contourf(topo1,100,cmap=my_cm,norm=norm1)
      plt.grid(linestyle='-',linewidth=0.50)
      
      # Colorbar 
      ax2 = fig.add_axes([0.435, 0.09, 0.018, 0.75])
      CB1 = mpl.colorbar.ColorbarBase(ax2, cmap=my_cm, norm=norm1, orientation='vertical')
      CB1.set_label('[%]')

      Comment

      Working...