ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 13 Apr 2019 23:20:42 +0200colorbar and axis labels overlayed - how to fix? (or "how to give parameters to save that i can give to show?")https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/[This](https://ask.sagemath.org/question/46117/how-to-label-your-colorbar-in-contour-plots/) helped me to fix the label on my colorbar, and I have consistent fonts now, too. yay.
however two issues remain:
# Region where contour lines should be plotted
def region_to_include(x,y):
return (x-.5)^2+(y-.5)^2>0.01 and (x+.5)^2+(y-.5)^2>0.01 and (x-.5)^2+(y+.5)^2>0.01 and (x+.5)^2+(y+.5)^2>0.01
# Region to be excluded
def region_to_exclude(x,y):
return not region_to_include(x,y)
#=================== visual plausibility check
pic=phi_ges.subs({
Q : 1e-9,
d_e : 1,
z_s : .25,
z_c : .5,
epsilon_m : 76,
epsilon_s : 21.75,
epsilon_0 : 8.854187817e-12
})
from matplotlib import rc, rcParams
rc('text',usetex=True)
rc('font',**{'family':'serif','serif':['Computer Modern']})
c = 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]'], typeset='latex', frame=True, axes=False)
plt=c[0]
opt = plt.options()
opt['colorbar_options']['label'] = 'Potential in $V$'
plt.set_options(opt)
c[0] = plt
# Plot of the region to be excluded
r = region_plot(region_to_exclude, (x,-1,1), (z,-1,1),
plot_points=300, incol="gray", bordercol="black", borderwidth=1)
# Final plot
show(c + r,)
bild=c + r
bild.save('potentialfeld.pdf')
gives me a plot like this:
![plot with lables overlayed and two regions white, not grey](/upfiles/15551458435677226.png)
why does my `frame=True, axes=False` get ignored in the first plot command? It does work if i add those parameters to a separate show command, like `show(c+r, frame=True, axes=False)`, but then i can't save it like that.
saving the plot manually from within the viewer that show() spawns gives me this pic, as i want it. but i would like to be able to save it from sage directly.
![pic as i want it, saved manually](/upfiles/15551859141144462.png)
how to fix?Sat, 13 Apr 2019 11:00:45 +0200https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/Comment by stockh0lm for <p><a href="https://ask.sagemath.org/question/46117/how-to-label-your-colorbar-in-contour-plots/">This</a> helped me to fix the label on my colorbar, and I have consistent fonts now, too. yay.</p>
<p>however two issues remain: </p>
<pre><code># Region where contour lines should be plotted
def region_to_include(x,y):
return (x-.5)^2+(y-.5)^2>0.01 and (x+.5)^2+(y-.5)^2>0.01 and (x-.5)^2+(y+.5)^2>0.01 and (x+.5)^2+(y+.5)^2>0.01
# Region to be excluded
def region_to_exclude(x,y):
return not region_to_include(x,y)
#=================== visual plausibility check
pic=phi_ges.subs({
Q : 1e-9,
d_e : 1,
z_s : .25,
z_c : .5,
epsilon_m : 76,
epsilon_s : 21.75,
epsilon_0 : 8.854187817e-12
})
from matplotlib import rc, rcParams
rc('text',usetex=True)
rc('font',**{'family':'serif','serif':['Computer Modern']})
c = 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]'], typeset='latex', frame=True, axes=False)
plt=c[0]
opt = plt.options()
opt['colorbar_options']['label'] = 'Potential in $V$'
plt.set_options(opt)
c[0] = plt
# Plot of the region to be excluded
r = region_plot(region_to_exclude, (x,-1,1), (z,-1,1),
plot_points=300, incol="gray", bordercol="black", borderwidth=1)
# Final plot
show(c + r,)
bild=c + r
bild.save('potentialfeld.pdf')
</code></pre>
<p>gives me a plot like this:</p>
<p><img alt="plot with lables overlayed and two regions white, not grey" src="/upfiles/15551458435677226.png"></p>
<p>why does my <code>frame=True, axes=False</code> get ignored in the first plot command? It does work if i add those parameters to a separate show command, like <code>show(c+r, frame=True, axes=False)</code>, but then i can't save it like that. </p>
<p>saving the plot manually from within the viewer that show() spawns gives me this pic, as i want it. but i would like to be able to save it from sage directly. </p>
<p><img alt="pic as i want it, saved manually" src="/upfiles/15551859141144462.png"></p>
<p>how to fix?</p>
https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/?comment=46141#post-id-46141before i had also asked about an issue with the poles on the left being not greyed out, i found and fixed the typo.Sat, 13 Apr 2019 22:01:47 +0200https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/?comment=46141#post-id-46141Answer by dsejas for <p><a href="https://ask.sagemath.org/question/46117/how-to-label-your-colorbar-in-contour-plots/">This</a> helped me to fix the label on my colorbar, and I have consistent fonts now, too. yay.</p>
<p>however two issues remain: </p>
<pre><code># Region where contour lines should be plotted
def region_to_include(x,y):
return (x-.5)^2+(y-.5)^2>0.01 and (x+.5)^2+(y-.5)^2>0.01 and (x-.5)^2+(y+.5)^2>0.01 and (x+.5)^2+(y+.5)^2>0.01
# Region to be excluded
def region_to_exclude(x,y):
return not region_to_include(x,y)
#=================== visual plausibility check
pic=phi_ges.subs({
Q : 1e-9,
d_e : 1,
z_s : .25,
z_c : .5,
epsilon_m : 76,
epsilon_s : 21.75,
epsilon_0 : 8.854187817e-12
})
from matplotlib import rc, rcParams
rc('text',usetex=True)
rc('font',**{'family':'serif','serif':['Computer Modern']})
c = 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]'], typeset='latex', frame=True, axes=False)
plt=c[0]
opt = plt.options()
opt['colorbar_options']['label'] = 'Potential in $V$'
plt.set_options(opt)
c[0] = plt
# Plot of the region to be excluded
r = region_plot(region_to_exclude, (x,-1,1), (z,-1,1),
plot_points=300, incol="gray", bordercol="black", borderwidth=1)
# Final plot
show(c + r,)
bild=c + r
bild.save('potentialfeld.pdf')
</code></pre>
<p>gives me a plot like this:</p>
<p><img alt="plot with lables overlayed and two regions white, not grey" src="/upfiles/15551458435677226.png"></p>
<p>why does my <code>frame=True, axes=False</code> get ignored in the first plot command? It does work if i add those parameters to a separate show command, like <code>show(c+r, frame=True, axes=False)</code>, but then i can't save it like that. </p>
<p>saving the plot manually from within the viewer that show() spawns gives me this pic, as i want it. but i would like to be able to save it from sage directly. </p>
<p><img alt="pic as i want it, saved manually" src="/upfiles/15551859141144462.png"></p>
<p>how to fix?</p>
https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/?answer=46142#post-id-46142Hello, @stockh0lm. I am glad that you could use my last answer.
Let me give you a couple of solutions to your problems. I'll start with the easiest: As far as I'm aware, the `frame` and `axes` options are set globally. So when creating plot `r`, you are automatically overwriting these options with the defaults, namely `frame=False` and `axes=True`. One way to solve this is just adding your preferences to the command that defines `r`:
r = region_plot(region_to_exclude, (x,-1,1), (z,-1,1),
plot_points=300, incol="gray", bordercol="black", borderwidth=1, frame=True, axes=False)
That will solve your problem. However, a more elegant way to do this would be to use the same options you have put in `show`, but in `save`, i.e., the `save` command accepts the same options as the `show` command. In your case, you can use
bild.save('potentialfeld.pdf', axes=False, frame=True)
That way you can save time by not defining these options for `c` and `r` repeatedly. (By the way, you don't need the line `show(c + r,)`.) The result should be as follows (I didn't have the definition of your `pic`, so I used another function, but the idea is there):
![image description](/upfiles/15551900034454099.png)
(The code is located [in this Sage Cell](https://sagecell.sagemath.org/?z=eJx1U0tr3DAQvhv8H4ZkQXZWMbsbellQL4XcCjmUXoxjZFneqJUlI8mpd0v_e8eP3WxoIzAazXvm-_zKXUJOJI2jTolkoKcUGAzPu_XpeRdHcVTLBpw8KGvKYEs5CN3XEv2O6T6OAI-ToXcGjA1Xfsq8-Y1ZGmdbaHnotA1aVaDazjoMEBS_J-546-PIiYQEOQRCey9RYN9cL9NZ31iD-ru736ThrdJHsideOtUQutz7nHyxbdcH6eCrraUzpPgz1Y4jgSMJTGB7V44dJDgqBWzufku3KUqns7R4eQYPGwqjb9lZZYJnuw0qRMs7Jqy2DgWf_ZBhDMFnxd3ULQU-SF9qXkntWU5WwwqUgbwtsNHV6fIoKIRjJ3FMRjTHWScAdGAi3xRxZLuALeM7QwlX6pN0UubkXK1cDKTIyVSNFBhBnmyQJiiux0Kr7ysyZc2wzjkgwRuTjXXmEvOK3sN8Dd97mJPhPvuUIj2S4yx83mSbLXBTo2n9sekStf446so0tnQLT7h_sA2EF7k0B8FCJWGhYY3cwCGWvidk_6Hq_3GeZ7o-11g_jFjjDqxmNwfHjzcUKuuQU5Om0lz8vKh-qTq8sO3CtFt4VAa3P2aLo0rpmglYg5vlzPNXmZDuDFIjUdeZA6EN_gFyZtBIIPbItZfpXze8G7U=&lang=sage&interacts=eJyLjgUAARUAuQ==))
Now, let's try to solve the $x$ axis label problem. This is a little more complicated (I think). In this case, since you are not plotting axes, the $x$ label should go automatically to the bottom, so it will not interfere anymore.
However, in case you do have a similar problem with another plot, I can suggest two alternatives. First, you could reposition the colorbar. If you want to do this, just add the line
opt['colorbar_options']['orientation'] = 'horizontal'
just after the line `opt['colorbar_options']['label'] = 'Potential in $V$'` (or just before, in any case). This will put the colorbar bellow your plot, so it will not interfere with axis labels.
A less elegant solution, but useful in some occations is to add line-breaks to the axis labels. Instead of putting `axes_labels=['$x$ in [m]','$z$ in [m]']`as an option for plot `p`, you could use `axes_labels=['$x$\nin\n[m]','$z$ in [m]']`. The `\n` defines a line-break in that precise place.Sat, 13 Apr 2019 23:14:16 +0200https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/?answer=46142#post-id-46142Comment by stockh0lm for <p>Hello, <a href="/users/25843/stockh0lm/">@stockh0lm</a>. I am glad that you could use my last answer.</p>
<p>Let me give you a couple of solutions to your problems. I'll start with the easiest: As far as I'm aware, the <code>frame</code> and <code>axes</code> options are set globally. So when creating plot <code>r</code>, you are automatically overwriting these options with the defaults, namely <code>frame=False</code> and <code>axes=True</code>. One way to solve this is just adding your preferences to the command that defines <code>r</code>:</p>
<pre><code>r = region_plot(region_to_exclude, (x,-1,1), (z,-1,1),
plot_points=300, incol="gray", bordercol="black", borderwidth=1, frame=True, axes=False)
</code></pre>
<p>That will solve your problem. However, a more elegant way to do this would be to use the same options you have put in <code>show</code>, but in <code>save</code>, i.e., the <code>save</code> command accepts the same options as the <code>show</code> command. In your case, you can use</p>
<pre><code>bild.save('potentialfeld.pdf', axes=False, frame=True)
</code></pre>
<p>That way you can save time by not defining these options for <code>c</code> and <code>r</code> repeatedly. (By the way, you don't need the line <code>show(c + r,)</code>.) The result should be as follows (I didn't have the definition of your <code>pic</code>, so I used another function, but the idea is there):
<img alt="image description" src="/upfiles/15551900034454099.png"></p>
<p>(The code is located <a href="https://sagecell.sagemath.org/?z=eJx1U0tr3DAQvhv8H4ZkQXZWMbsbellQL4XcCjmUXoxjZFneqJUlI8mpd0v_e8eP3WxoIzAazXvm-_zKXUJOJI2jTolkoKcUGAzPu_XpeRdHcVTLBpw8KGvKYEs5CN3XEv2O6T6OAI-ToXcGjA1Xfsq8-Y1ZGmdbaHnotA1aVaDazjoMEBS_J-546-PIiYQEOQRCey9RYN9cL9NZ31iD-ru736ThrdJHsideOtUQutz7nHyxbdcH6eCrraUzpPgz1Y4jgSMJTGB7V44dJDgqBWzufku3KUqns7R4eQYPGwqjb9lZZYJnuw0qRMs7Jqy2DgWf_ZBhDMFnxd3ULQU-SF9qXkntWU5WwwqUgbwtsNHV6fIoKIRjJ3FMRjTHWScAdGAi3xRxZLuALeM7QwlX6pN0UubkXK1cDKTIyVSNFBhBnmyQJiiux0Kr7ysyZc2wzjkgwRuTjXXmEvOK3sN8Dd97mJPhPvuUIj2S4yx83mSbLXBTo2n9sekStf446so0tnQLT7h_sA2EF7k0B8FCJWGhYY3cwCGWvidk_6Hq_3GeZ7o-11g_jFjjDqxmNwfHjzcUKuuQU5Om0lz8vKh-qTq8sO3CtFt4VAa3P2aLo0rpmglYg5vlzPNXmZDuDFIjUdeZA6EN_gFyZtBIIPbItZfpXze8G7U=&lang=sage&interacts=eJyLjgUAARUAuQ==">in this Sage Cell</a>)</p>
<p>Now, let's try to solve the $x$ axis label problem. This is a little more complicated (I think). In this case, since you are not plotting axes, the $x$ label should go automatically to the bottom, so it will not interfere anymore.</p>
<p>However, in case you do have a similar problem with another plot, I can suggest two alternatives. First, you could reposition the colorbar. If you want to do this, just add the line</p>
<pre><code>opt['colorbar_options']['orientation'] = 'horizontal'
</code></pre>
<p>just after the line <code>opt['colorbar_options']['label'] = 'Potential in $V$'</code> (or just before, in any case). This will put the colorbar bellow your plot, so it will not interfere with axis labels.</p>
<p>A less elegant solution, but useful in some occations is to add line-breaks to the axis labels. Instead of putting <code>axes_labels=['$x$ in [m]','$z$ in [m]']</code>as an option for plot <code>p</code>, you could use <code>axes_labels=['$x$\nin\n[m]','$z$ in [m]']</code>. The <code>\n</code> defines a line-break in that precise place.</p>
https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/?comment=46143#post-id-46143awesome and in depth! thank you!Sat, 13 Apr 2019 23:20:42 +0200https://ask.sagemath.org/question/46122/colorbar-and-axis-labels-overlayed-how-to-fix-or-how-to-give-parameters-to-save-that-i-can-give-to-show/?comment=46143#post-id-46143