Open williamstein opened 17 years ago
Attachment: trac_260-transparent_graphics.patch.gz
The attached patch adds a keyword argument transparency
to plot.show()
. The default value is None
, which makes the image background opaque. A number between 0 (transparent) and 1 (opaque) determines the degree of transparency.
Please test and make changes. I'm new to the plotting code, so it's likely that I've missed and/or broken something.
Attachment: trac_260-transparent_graphics_v2.patch.gz
Added examples.
Perhaps opacity
is a more appropriate keyword (alpha
gave errors).
Ticket #5448 may necessitate an update.
Yes, I'm almost sure that #5448 will necessitate an update (or this will necessitate an update of #5448).
I would change the keyword argument to something that is used more in Sage (like alpha or opacity) if we are going to have multiple levels of transparency. If it is just a True/False option, then "transparent" seems like a fine keyword.
What do you think about using the "transparent" option of savefig, as documented here: http://matplotlib.sourceforge.net/api/figure_api.html#matplotlib.figure.Figure.savefig ?
The transparent option of savefig has the following code. It looks like this code not only sets the figure patch, but goes through each axes object in the figure and sets the axes patch alpha level. We should probably do the same.
1036 if transparent:
1037 original_figure_alpha = self.patch.get_alpha()
1038 self.patch.set_alpha(0.0)
1039 original_axes_alpha = []
1040 for ax in self.axes:
1041 patch = ax.patch
1042 original_axes_alpha.append(patch.get_alpha())
1043 patch.set_alpha(0.0)
How about adapting savefig()
's code for a numerical scalar option with keyword alpha
? Or should we consider allowing color 4-tuples, e.g., RGBA, for different figure components and plotted objects? A disclaimer: I'm not very familiar with how matplotlib or Sage plotting works.
I'm happy to postpone this ticket's review until #5448 merges. Alternatively, I can recommend closing this one, if it's easier to treat transparency at #5448.
I added a quick "transparent" option to the patch at #5448. If you think it is needed, I think this ticket ought to go ahead and implement a "background_color" and/or "background_opacity" keywords, or something like that, that lets a user specify a background color and opacity.
Please look at the patch at #5448 and let me know if I didn't cover something you need done.
Description changed:
---
+++
@@ -2,5 +2,5 @@
It may be helpful for users who want to use SAGE graphics on their web -pages to be able to set the background as transparent. +pages to be able to set attributes about the background, like transparency.
FYI, #5448 did implement the transparency=True/False option to show. However, this ticket can have larger scope. I'm slightly enlarging the title/description to reflect that fact.
On the other hand, if it's not wanted, maybe we should close this ticket after all.
A quick (and crude) change to Graphics.save()
sets up a background_color
option:
if savenow:
[...]
background_color = None
if kwds.has_key('background_color'):
background_color = kwds.pop('background_color', False)
figure=self.matplotlib(*args, **kwds)
[...]
if background_color:
figure.patch.set_color(background_color)
for ax in figure.axes:
ax.patch.set_color(background_color)
# Not sure how to avoid using these:
options['edgecolor'] = background_color
options['facecolor'] = background_color
figure.savefig(filename,dpi=dpi,bbox_inches='tight',**options)
If we can avoid using the savefig()
options, perhaps we can set a background color and transparency level entirely in matplotlib()
. Then, I think, we could handle combinations like
transparent=True, opacity=0.5
opacity=0.8, background_color='#ffbefa'
in a way a user expects. Thoughts?
Add opacity and background_color plot options. Apply only this patch.
Attachment: trac_260-plot_bg_alpha.patch.gz
The new patch adds background_color
and opacity
keyword options to plot()
. Examples:
sage: plot(x^cos(x^(sin(x))), (0, 30), fill='axis', fillcolor='yellow', opacity=0.5)
sage: C = 1.0
sage: a, b = var('a, b')
sage: lem = contour_plot(2 * C^2 * (b^2 - a^2) - (a^2 + b^2)^2, (a, -2, 2), (b, -2, 2), plot_points=100, transparent=True, contours=25, cmap='Spectral')
sage: lem.show(aspect_ratio=1.0, background_color='khaki')
Can a Sage plotting or matplotlib expert point out how to make the background uniform when both background_color
and opacity
are given? Try this:
sage: plot(x^cos(x^(sin(x))), (0, 30), fill='axis', fillcolor='yellow', background_color='red', opacity=0.5)
Note how the plot's thick "border" has a different apparent transparency level. Is this an alpha compositing or blending problem?
Author: Mitesh Patel
It looks like there are two patches; one for the image, and one for the axes background. Each is set to 50% opacity, and they are layered on top of each other.
But post to the matplotlib mailing list. I'm sure they'll have a good answer for you.
Replying to @jasongrout:
But post to the matplotlib mailing list. I'm sure they'll have a good answer for you.
Oops. I didn't notice your response. I'll ask the matplotlib mavens.
Any responses?
No, because I stupidly neglected to ask. Sorry about this! I'll try to ask on the matplotlib mailing list soon, probably after we release Sage 4.5.2.
Replying to @qed777:
No, because I stupidly neglected to ask. Sorry about this! I'll try to ask on the matplotlib mailing list soon, probably after we release Sage 4.5.2.
Attachment: trac_260-plot_bg_alpha_rebased.patch.gz
Rebased. Applies cleanly to Sage 4.6.2.
Rebased for Sage 4.6.2 so it applies cleanly. Hopefully this helps with further testing.
If I recall correctly, the only issue was this image versus background thing, and the mpl devels in the thread above didn't have much to say that was helpful. If things look okay with this, maybe fixing whatever is left could be another ticket. It's sort of said we don't have this merged yet.
Reviewer: Jason Grout, Ryan Grout
Well, Jason, what do you think? I wonder if mpl has new ways of designating these things now... anyway, just pinging about the status of this.
CC: @kcrisman
Component: graphics
Author: Mitesh Patel
Reviewer: Jason Grout, Ryan Grout
Issue created by migration from https://trac.sagemath.org/ticket/260