0

My data changes. I want the color bar to remain the same. Here is some sample code:

    x1 = np.linspace(-10, 10, 100)
    x2 = np.linspace(-10, 10, 100)
    X1, X2 = np.meshgrid(x1, x2)

    Y1 = np.sqrt(np.square(X1) + np.square(X2))  # want the gradient bar always based on this
    Y2 = np.sqrt(np.square(X1))  # sometimes the data changes, but want same gradient bar

    figur = 2
    if figur == 1:
        colormap = Y1
    elif figur == 2:
       colormap = Y2

    cm = plt.cm.get_cmap('viridis')
    plt.scatter(X1, X2, c=colormap, cmap=cm)
    v = np.arange(0, 14.2, 2)
    cbar = plt.colorbar(ticks=v)
    cbar.set_ticks(v)
    cbar.set_ticklabels(v)
    plt.savefig('fig' + str(figur) + '.png')
    plt.show()

Here are the plots. When figur in the code is 1 or 2, there is different gradient data being plotted. However, I would like for the gradient color bar to remain the same (0, 14.2) for both plots. Figure 1

Figure 2

How can I set the gradient bar to be constant, arange(0, 14.2, 2) for multiple plots even when the gradient data currently being plotted only goes to say 10?

GeneralCode
  • 794
  • 1
  • 11
  • 26
  • 1
    Does this answer your question? [Set Colorbar Range in matplotlib](https://stackoverflow.com/questions/3373256/set-colorbar-range-in-matplotlib) – eike Jan 22 '20 at 18:10
  • Yes it absolutely answers the question. vmin and vmax, thank you! – GeneralCode Jan 22 '20 at 18:19

0 Answers0