sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.41k stars 475 forks source link

Background color and opacity of graphics output #260

Open williamstein opened 17 years ago

williamstein commented 17 years ago

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.

CC: @kcrisman

Component: graphics

Author: Mitesh Patel

Reviewer: Jason Grout, Ryan Grout

Issue created by migration from https://trac.sagemath.org/ticket/260

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago
comment:2

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.

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago

Attachment: trac_260-transparent_graphics_v2.patch.gz

Added examples.

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago
comment:3

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

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago
comment:4

Ticket #5448 may necessitate an update.

jasongrout commented 15 years ago
comment:5

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.

  2. 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 ?

  3. 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)
e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago
comment:6

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.

jasongrout commented 15 years ago
comment:7

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.

jasongrout commented 15 years ago

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.

jasongrout commented 15 years ago
comment:9

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.

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago
comment:10

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

in a way a user expects. Thoughts?

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago

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

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago
comment:11

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?

e14f4152-4982-4ace-8c95-73a0599b109b commented 15 years ago

Author: Mitesh Patel

jasongrout commented 15 years ago
comment:12

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.

e14f4152-4982-4ace-8c95-73a0599b109b commented 14 years ago
comment:14

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.

kcrisman commented 14 years ago
comment:15

Any responses?

e14f4152-4982-4ace-8c95-73a0599b109b commented 14 years ago
comment:16

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.

e14f4152-4982-4ace-8c95-73a0599b109b commented 14 years ago
comment:17

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.

I've posted to matplotlib-users.

cb31b68b-3181-4f46-ab62-081a947f4434 commented 13 years ago

Attachment: trac_260-plot_bg_alpha_rebased.patch.gz

Rebased. Applies cleanly to Sage 4.6.2.

cb31b68b-3181-4f46-ab62-081a947f4434 commented 13 years ago
comment:18

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

kcrisman commented 13 years ago
comment:19

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.

kcrisman commented 13 years ago

Reviewer: Jason Grout, Ryan Grout

kcrisman commented 11 years ago
comment:20

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.