3

I have a question regarding the colorbar of the matplotlib. I have a surface-plot, which is working fine, and the colors are used correctly. But somehow, the scale of my colorbar is messed up. I think it should go from 0 to 0.4. But the actual code gives me 0 to 0.16. What am I missing here? Strange that the value 0.16 is the square of 0.4.

Here is my plot: enter image description here

And of course, here is my code:

values_all = zip(*values_all)
x = range(len(values_all[0]))
y = range(len(values_all))
figure = plt.figure(1, figsize=(10, 7))
ax = Axes3D(figure, azim=-124, elev=40, zlim=(0, 0.4))
x, y = np.meshgrid(x, y)
surface = ax.plot_surface(x, y, values_all, linewidth=0, rstride=1, cstride=1, cmap=cm.jet)
plt.colorbar(surface, shrink=0.4, aspect=10)
plt.show()

If I edit my code the following way, the colorbar is scaled correctly, but the plot itself is not colored correctly anymore:

surface = ax.plot_surface(x, y, values_all, linewidth=0, rstride=1, cstride=1, cmap=cm.jet,vmin=0,vmax=0.4)

Results in: enter image description here

With other sample data, you can see that this is not only an issue of the plot's borders (The values that are giving the strange peak are between 0.3-0.35):

enter image description here

PKlumpp
  • 4,913
  • 8
  • 36
  • 64
  • Never used matplotlib however, as you note that 0.16 is 0.4 x 0.4 and you have two 0.4s in your code, have you tried changing one of them (e.g. the `shrink`) to see if that changes anything? – elParaguayo Jul 24 '14 at 07:34
  • @elParaguayo good Point. I tried this already. Tried it again just now, but this does really only change the size of the bar. the values remain the same :-/ – PKlumpp Jul 24 '14 at 07:38
  • OK - then I'll leave to someone who knows matplotlib. Good luck. – elParaguayo Jul 24 '14 at 07:39
  • What do you get if you check the max of `values_all`? – deinonychusaur Jul 24 '14 at 07:45
  • @deinonychusaur it is at about 0.349[...]. There are several values over 0.16 – PKlumpp Jul 24 '14 at 07:50
  • It is a bit hard without sample data to work with, but could be that this would work: http://stackoverflow.com/a/4366663/1099682 – deinonychusaur Jul 24 '14 at 08:03
  • if you need sample data, you could just hardcore a list of 3 lists. each of the list contains 3 values. so something like `[[0.1,0.2,0.1],[0.1,0.35,0.2],[0.3,0.1,0.1]]`. Thanks for your help! – PKlumpp Jul 24 '14 at 08:08
  • It kind of looks like your plot is using the colorbar shown. Maybe try setting `vmin=0,vmax=0.4` in the `plot_surface` command. – Christoph Jul 24 '14 at 08:17
  • @Christoph see my edit... very strange – PKlumpp Jul 24 '14 at 08:21
  • What exactly do you think is wrong with the coloring? The only odd thing I see are the colors of the spikes on the boundary. I am not sure how mplot3d deals with isolated points like this. Might be a rendering issue. If that is the case, you might try linearly interpolating your data to a finer grid before plotting. – Christoph Jul 24 '14 at 08:33
  • @Christoph ok I checked this once more. The coloring is also messy in the middle of the plot. I have created some sample data. and will post the result. sec – PKlumpp Jul 24 '14 at 08:47
  • @PKlumpp have you found the solution for this? I'm having the exact same problem as you. Thank you – VMMF Mar 29 '23 at 16:45

0 Answers0