Ask Your Question

How to label your colorbar in contour plots?

asked 2019-04-12 16:41:28 -0500

stockh0lm gravatar image

updated 2019-04-12 16:42:05 -0500

i looked at the options of the contour_plot funciton and tried this:

contour_plot(pic, (x,-1,1), (z,-1,1), region=region_to_include, contours= 30, plot_points=200, cmap=colormaps.jet, colorbar=True, axes_labels=['$x$ in [m]','$z$ in [m]'], legend_label=['Potential in $V$'])

but that does nto give any effect.

I want to put the unit of my plot there. how can i do that?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-12 21:23:52 -0500

dsejas gravatar image

updated 2019-04-12 22:52:56 -0500

Hello. What I understand is that you want to put "Potential in $V$" as your colorbar legend (?). I have a little complicated form, but I think it will help you. Let me give you an example smaller than your plot:

p = contour_plot(x^2+y^2,(x,-1,1), (y,-1,1), cmap=colormaps.jet, colorbar=True)

Now you have stored you contour plot inside the variable p. This variable actually is a list of plots (in this case, it just contains your single plot in p[0], but if you were to make something like p+=plot(x^2), then this second graphic would be stores in p[1], and so on). Let's separate your plot in another variable; you do this with:

plt = p[0]

Now you can access all the options of your plot. For example, if you type:

opt = plt.options()

you will see there is a part labeled "colorbar_options"; if you don't see it, you can just replace the last line with


This is a Python dict. We will add a field called "label" which will be the label of your colorbar. In this case:

opt['colorbar_options']['label'] = 'Potential in $V$'

That's it. Now you have to do the same steps backwards to apply the new options:

p[0] = plt

Finally, you plot it:

The result should be like this: image description

You can also check the whole code in this Sage Cell page: Contour plot with labelled colorbar

I'm sorry I didn't find another easier way to do this. However, you can wrap this code in a function (if you can't do it, let me know so I can send it to you). If I find another way, I'll write it.

I hope this helps!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-04-12 16:41:28 -0500

Seen: 36 times

Last updated: Apr 12