Ticket #260 (needs_work enhancement)
Background color and opacity of graphics output
| Reported by: | was | Owned by: | was |
|---|---|---|---|
| Priority: | minor | Milestone: | sage-5.10 |
| Component: | graphics | Keywords: | |
| Cc: | kcrisman | Work issues: | |
| Report Upstream: | N/A | Reviewers: | Jason Grout, Ryan Grout |
| Authors: | Mitesh Patel | Merged in: | |
| Dependencies: | Stopgaps: |
Description (last modified by jason) (diff)
It may be helpful for users who want to use SAGE graphics on their web pages to be able to set attributes about the background, like transparency.
Attachments
Change History
comment:2 Changed 4 years ago by mpatel
- Summary changed from transparent graphics output to [with patch, needs review] transparent graphics output
- Milestone changed from sage-feature to sage-4.1.2
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.
Changed 4 years ago by mpatel
-
attachment
trac_260-transparent_graphics_v2.patch
added
Added examples.
comment:3 Changed 4 years ago by mpatel
- Owner changed from boothby to was
- Component changed from notebook to graphics
Perhaps opacity is a more appropriate keyword (alpha gave errors).
comment:5 Changed 4 years ago by jason
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)
comment:6 Changed 4 years ago by mpatel
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.
comment:7 Changed 4 years ago by jason
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.
comment:8 Changed 4 years ago by mpatel
- Summary changed from [with patch, needs review] transparent graphics output to [with patch, needs work] transparent graphics output
comment:9 Changed 4 years ago by jason
- Description modified (diff)
- Summary changed from [with patch, needs work] transparent graphics output to [with patch, needs work] background of graphics output
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.
comment:10 Changed 4 years ago by mpatel
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?
Changed 4 years ago by mpatel
-
attachment
trac_260-plot_bg_alpha.patch
added
Add opacity and background_color plot options. Apply only this patch.
comment:11 Changed 4 years ago by mpatel
- Cc kcrisman added
- Status changed from needs_work to needs_review
- Summary changed from [with patch, needs work] background of graphics output to Background color and opacity of graphics output
- Authors set to Mitesh Patel
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?
comment:12 follow-up: ↓ 14 Changed 4 years ago by jason
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.
comment:13 Changed 3 years ago by mpatel
- Status changed from needs_review to needs_work
- Report Upstream set to N/A
comment:14 in reply to: ↑ 12 Changed 3 years ago by mpatel
Replying to jason:
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.
comment:15 Changed 3 years ago by kcrisman
Any responses?
comment:16 follow-up: ↓ 17 Changed 3 years ago by mpatel
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.
comment:17 in reply to: ↑ 16 Changed 3 years ago by mpatel
Replying to mpatel:
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.
Changed 2 years ago by ryan
-
attachment
trac_260-plot_bg_alpha_rebased.patch
added
Rebased. Applies cleanly to Sage 4.6.2.
comment:18 Changed 2 years ago by ryan
Rebased for Sage 4.6.2 so it applies cleanly. Hopefully this helps with further testing.
comment:19 Changed 2 years ago by kcrisman
- Reviewers set to Jason Grout, Ryan Grout
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.
comment:20 Changed 4 months ago by kcrisman
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.
