rescaling a matplotlib.colorbar and matplotlib.contour after creation












4















I have a contourplot, generated using matplotlib.



import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = XX**2 + YY**2
levels = np.linspace(0, 200, 10)

fig, ax = plt.subplots()
cf = ax.contourf(X, Y, ZZ, levels=levels)
cbar = fig.colorbar(cf)


This generates this figure:



enter image description here



Now imagine, that for some reason, the figure is handed to the user through some functions call, and the user only gets the ax, fig, and cbar objects.



UPDATE: The reason I want to do this, is because I have to make a gargantuan amount of contourplots in an automated way. I have automated the creation of the contourplot in the backend (vaguely described as "the function call" here above). But for some plots, some slight modification is required to generated exquisite plots. So instead of manipulating the backed or having to add numerous amounts of options for every situation, it would be nice if the couple of figures I need to adjust out of the +400 figures are adjustable through the ax, fig, and cbar objects.



Now only manipulating those objects, I want to zoom in, and rescale both the colorbar as well as the levels.



Zooming in:



ax.set_xlim(2.5, 7.5)
ax.set_ylim(2.5, 7.5)


enter image description here



Changing the limit on the colorbar and the ticks:



new_clim = (0, 100)
# find location of the new upper limit on the color bar
loc_on_cbar = cbar.norm(new_clim[1])
# redefine limits of the colorbar
cf.colorbar.set_clim(*new_clim)
cf.colorbar.set_ticks(np.linspace(*new_clim, 10))
# redefine the limits of the levels of the contour
cf.set_clim(*new_clim)
# updating the contourplot
cf.changed()


enter image description here



Changing the scale of the colorbar itself:



cbar.ax.set_ylim(0, loc_on_cbar)
cbar.ax.autoscale_view()


enter image description here



Now there are some issues here:




  1. I cannot seem the find how to rescale the colorbar's axis without having that annoying empty rectangle on top.

  2. I cannot find howto adjust the number of levels in the contourplot & colorbar after creating the instance.


Thanks a lot!










share|improve this question

























  • I don't think that you can change the number of levels of cf after creation. The contourf function returns a QuadContourSet, which, I think, doesn't have any information on the function from which the contours are derived.

    – Thomas Kühn
    Nov 23 '18 at 10:00











  • If you know which plots should be altered before creating them, simply changing the creation method to zooming in on the right location in the first place would be the easiest way to go.

    – cheersmate
    Nov 23 '18 at 11:18











  • For the colorbar, have you tried only calling cf.set_clim() and leaving out the other calls (i.e. cf.colorbar.set_clim() and so forth) and then calling fig.canvas.draw() after that?

    – Thomas Kühn
    Nov 23 '18 at 12:44











  • @ThomasKühn: Yes, this yields the same result as in figure 3. [Thanks for the tip on the QuadContourSet, I'll check it out.]

    – Daan
    Nov 23 '18 at 12:51


















4















I have a contourplot, generated using matplotlib.



import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = XX**2 + YY**2
levels = np.linspace(0, 200, 10)

fig, ax = plt.subplots()
cf = ax.contourf(X, Y, ZZ, levels=levels)
cbar = fig.colorbar(cf)


This generates this figure:



enter image description here



Now imagine, that for some reason, the figure is handed to the user through some functions call, and the user only gets the ax, fig, and cbar objects.



UPDATE: The reason I want to do this, is because I have to make a gargantuan amount of contourplots in an automated way. I have automated the creation of the contourplot in the backend (vaguely described as "the function call" here above). But for some plots, some slight modification is required to generated exquisite plots. So instead of manipulating the backed or having to add numerous amounts of options for every situation, it would be nice if the couple of figures I need to adjust out of the +400 figures are adjustable through the ax, fig, and cbar objects.



Now only manipulating those objects, I want to zoom in, and rescale both the colorbar as well as the levels.



Zooming in:



ax.set_xlim(2.5, 7.5)
ax.set_ylim(2.5, 7.5)


enter image description here



Changing the limit on the colorbar and the ticks:



new_clim = (0, 100)
# find location of the new upper limit on the color bar
loc_on_cbar = cbar.norm(new_clim[1])
# redefine limits of the colorbar
cf.colorbar.set_clim(*new_clim)
cf.colorbar.set_ticks(np.linspace(*new_clim, 10))
# redefine the limits of the levels of the contour
cf.set_clim(*new_clim)
# updating the contourplot
cf.changed()


enter image description here



Changing the scale of the colorbar itself:



cbar.ax.set_ylim(0, loc_on_cbar)
cbar.ax.autoscale_view()


enter image description here



Now there are some issues here:




  1. I cannot seem the find how to rescale the colorbar's axis without having that annoying empty rectangle on top.

  2. I cannot find howto adjust the number of levels in the contourplot & colorbar after creating the instance.


Thanks a lot!










share|improve this question

























  • I don't think that you can change the number of levels of cf after creation. The contourf function returns a QuadContourSet, which, I think, doesn't have any information on the function from which the contours are derived.

    – Thomas Kühn
    Nov 23 '18 at 10:00











  • If you know which plots should be altered before creating them, simply changing the creation method to zooming in on the right location in the first place would be the easiest way to go.

    – cheersmate
    Nov 23 '18 at 11:18











  • For the colorbar, have you tried only calling cf.set_clim() and leaving out the other calls (i.e. cf.colorbar.set_clim() and so forth) and then calling fig.canvas.draw() after that?

    – Thomas Kühn
    Nov 23 '18 at 12:44











  • @ThomasKühn: Yes, this yields the same result as in figure 3. [Thanks for the tip on the QuadContourSet, I'll check it out.]

    – Daan
    Nov 23 '18 at 12:51
















4












4








4


1






I have a contourplot, generated using matplotlib.



import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = XX**2 + YY**2
levels = np.linspace(0, 200, 10)

fig, ax = plt.subplots()
cf = ax.contourf(X, Y, ZZ, levels=levels)
cbar = fig.colorbar(cf)


This generates this figure:



enter image description here



Now imagine, that for some reason, the figure is handed to the user through some functions call, and the user only gets the ax, fig, and cbar objects.



UPDATE: The reason I want to do this, is because I have to make a gargantuan amount of contourplots in an automated way. I have automated the creation of the contourplot in the backend (vaguely described as "the function call" here above). But for some plots, some slight modification is required to generated exquisite plots. So instead of manipulating the backed or having to add numerous amounts of options for every situation, it would be nice if the couple of figures I need to adjust out of the +400 figures are adjustable through the ax, fig, and cbar objects.



Now only manipulating those objects, I want to zoom in, and rescale both the colorbar as well as the levels.



Zooming in:



ax.set_xlim(2.5, 7.5)
ax.set_ylim(2.5, 7.5)


enter image description here



Changing the limit on the colorbar and the ticks:



new_clim = (0, 100)
# find location of the new upper limit on the color bar
loc_on_cbar = cbar.norm(new_clim[1])
# redefine limits of the colorbar
cf.colorbar.set_clim(*new_clim)
cf.colorbar.set_ticks(np.linspace(*new_clim, 10))
# redefine the limits of the levels of the contour
cf.set_clim(*new_clim)
# updating the contourplot
cf.changed()


enter image description here



Changing the scale of the colorbar itself:



cbar.ax.set_ylim(0, loc_on_cbar)
cbar.ax.autoscale_view()


enter image description here



Now there are some issues here:




  1. I cannot seem the find how to rescale the colorbar's axis without having that annoying empty rectangle on top.

  2. I cannot find howto adjust the number of levels in the contourplot & colorbar after creating the instance.


Thanks a lot!










share|improve this question
















I have a contourplot, generated using matplotlib.



import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-10, 10, 100)
Y = np.linspace(-10, 10, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = XX**2 + YY**2
levels = np.linspace(0, 200, 10)

fig, ax = plt.subplots()
cf = ax.contourf(X, Y, ZZ, levels=levels)
cbar = fig.colorbar(cf)


This generates this figure:



enter image description here



Now imagine, that for some reason, the figure is handed to the user through some functions call, and the user only gets the ax, fig, and cbar objects.



UPDATE: The reason I want to do this, is because I have to make a gargantuan amount of contourplots in an automated way. I have automated the creation of the contourplot in the backend (vaguely described as "the function call" here above). But for some plots, some slight modification is required to generated exquisite plots. So instead of manipulating the backed or having to add numerous amounts of options for every situation, it would be nice if the couple of figures I need to adjust out of the +400 figures are adjustable through the ax, fig, and cbar objects.



Now only manipulating those objects, I want to zoom in, and rescale both the colorbar as well as the levels.



Zooming in:



ax.set_xlim(2.5, 7.5)
ax.set_ylim(2.5, 7.5)


enter image description here



Changing the limit on the colorbar and the ticks:



new_clim = (0, 100)
# find location of the new upper limit on the color bar
loc_on_cbar = cbar.norm(new_clim[1])
# redefine limits of the colorbar
cf.colorbar.set_clim(*new_clim)
cf.colorbar.set_ticks(np.linspace(*new_clim, 10))
# redefine the limits of the levels of the contour
cf.set_clim(*new_clim)
# updating the contourplot
cf.changed()


enter image description here



Changing the scale of the colorbar itself:



cbar.ax.set_ylim(0, loc_on_cbar)
cbar.ax.autoscale_view()


enter image description here



Now there are some issues here:




  1. I cannot seem the find how to rescale the colorbar's axis without having that annoying empty rectangle on top.

  2. I cannot find howto adjust the number of levels in the contourplot & colorbar after creating the instance.


Thanks a lot!







python matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:01







Daan

















asked Nov 23 '18 at 8:41









DaanDaan

800819




800819













  • I don't think that you can change the number of levels of cf after creation. The contourf function returns a QuadContourSet, which, I think, doesn't have any information on the function from which the contours are derived.

    – Thomas Kühn
    Nov 23 '18 at 10:00











  • If you know which plots should be altered before creating them, simply changing the creation method to zooming in on the right location in the first place would be the easiest way to go.

    – cheersmate
    Nov 23 '18 at 11:18











  • For the colorbar, have you tried only calling cf.set_clim() and leaving out the other calls (i.e. cf.colorbar.set_clim() and so forth) and then calling fig.canvas.draw() after that?

    – Thomas Kühn
    Nov 23 '18 at 12:44











  • @ThomasKühn: Yes, this yields the same result as in figure 3. [Thanks for the tip on the QuadContourSet, I'll check it out.]

    – Daan
    Nov 23 '18 at 12:51





















  • I don't think that you can change the number of levels of cf after creation. The contourf function returns a QuadContourSet, which, I think, doesn't have any information on the function from which the contours are derived.

    – Thomas Kühn
    Nov 23 '18 at 10:00











  • If you know which plots should be altered before creating them, simply changing the creation method to zooming in on the right location in the first place would be the easiest way to go.

    – cheersmate
    Nov 23 '18 at 11:18











  • For the colorbar, have you tried only calling cf.set_clim() and leaving out the other calls (i.e. cf.colorbar.set_clim() and so forth) and then calling fig.canvas.draw() after that?

    – Thomas Kühn
    Nov 23 '18 at 12:44











  • @ThomasKühn: Yes, this yields the same result as in figure 3. [Thanks for the tip on the QuadContourSet, I'll check it out.]

    – Daan
    Nov 23 '18 at 12:51



















I don't think that you can change the number of levels of cf after creation. The contourf function returns a QuadContourSet, which, I think, doesn't have any information on the function from which the contours are derived.

– Thomas Kühn
Nov 23 '18 at 10:00





I don't think that you can change the number of levels of cf after creation. The contourf function returns a QuadContourSet, which, I think, doesn't have any information on the function from which the contours are derived.

– Thomas Kühn
Nov 23 '18 at 10:00













If you know which plots should be altered before creating them, simply changing the creation method to zooming in on the right location in the first place would be the easiest way to go.

– cheersmate
Nov 23 '18 at 11:18





If you know which plots should be altered before creating them, simply changing the creation method to zooming in on the right location in the first place would be the easiest way to go.

– cheersmate
Nov 23 '18 at 11:18













For the colorbar, have you tried only calling cf.set_clim() and leaving out the other calls (i.e. cf.colorbar.set_clim() and so forth) and then calling fig.canvas.draw() after that?

– Thomas Kühn
Nov 23 '18 at 12:44





For the colorbar, have you tried only calling cf.set_clim() and leaving out the other calls (i.e. cf.colorbar.set_clim() and so forth) and then calling fig.canvas.draw() after that?

– Thomas Kühn
Nov 23 '18 at 12:44













@ThomasKühn: Yes, this yields the same result as in figure 3. [Thanks for the tip on the QuadContourSet, I'll check it out.]

– Daan
Nov 23 '18 at 12:51







@ThomasKühn: Yes, this yields the same result as in figure 3. [Thanks for the tip on the QuadContourSet, I'll check it out.]

– Daan
Nov 23 '18 at 12:51














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443198%2frescaling-a-matplotlib-colorbar-and-matplotlib-contour-after-creation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443198%2frescaling-a-matplotlib-colorbar-and-matplotlib-contour-after-creation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out