Opened 13 years ago

Last modified 5 years ago

#260 needs_work enhancement

Background color and opacity of graphics output

Reported by: was Owned by: was
Priority: minor Milestone: sage-6.4
Component: graphics Keywords:
Cc: kcrisman Merged in:
Authors: Mitesh Patel Reviewers: Jason Grout, Ryan Grout
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by jason)

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 (4)

trac_260-transparent_graphics.patch (2.4 KB) - added by mpatel 10 years ago.
trac_260-transparent_graphics_v2.patch (3.2 KB) - added by mpatel 10 years ago.
Added examples.
trac_260-plot_bg_alpha.patch (5.6 KB) - added by mpatel 10 years ago.
Add opacity and background_color plot options. Apply only this patch.
trac_260-plot_bg_alpha_rebased.patch (4.5 KB) - added by ryan 8 years ago.
Rebased. Applies cleanly to Sage 4.6.2.

Download all attachments as: .zip

Change History (28)

comment:1 Changed 12 years ago by mabshoff

  • Milestone set to sage-feature

Changed 10 years ago by mpatel

comment:2 Changed 10 years ago by mpatel

  • Milestone changed from sage-feature to sage-4.1.2
  • Summary changed from transparent graphics output to [with patch, needs review] transparent graphics output

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 10 years ago by mpatel

Added examples.

comment:3 Changed 10 years ago by mpatel

  • Component changed from notebook to graphics
  • Owner changed from boothby to was

Perhaps opacity is a more appropriate keyword (alpha gave errors).

comment:4 Changed 10 years ago by mpatel

Ticket #5448 may necessitate an update.

comment:5 Changed 10 years ago by jason

Yes, I'm almost sure that #5448 will necessitate an update (or this will necessitate an update of #5448).

  1. 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.
  1. 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 ?
  1. 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 10 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 10 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 10 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 10 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 10 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 10 years ago by mpatel

Add opacity and background_color plot options. Apply only this patch.

comment:11 Changed 10 years ago by mpatel

  • Authors set to Mitesh Patel
  • 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

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: Changed 10 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 10 years ago by mpatel

  • Report Upstream set to N/A
  • Status changed from needs_review to needs_work

comment:14 in reply to: ↑ 12 Changed 10 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 9 years ago by kcrisman

Any responses?

comment:16 follow-up: Changed 9 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 9 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.

I've posted to matplotlib-users.

Changed 8 years ago by ryan

Rebased. Applies cleanly to Sage 4.6.2.

comment:18 Changed 8 years ago by ryan

Rebased for Sage 4.6.2 so it applies cleanly. Hopefully this helps with further testing.

comment:19 Changed 8 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 7 years 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.

comment:21 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:22 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:23 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:24 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.