mwshinn / CanD

6 stars 3 forks source link

Dark mode jupyter c.show() #8

Closed syncrostone closed 3 years ago

syncrostone commented 3 years ago

In jupyter dark mode, c.show() is hard to view because the text blends in with the background. It would be good to add a light grey splash or a white splash to the background of the canvas in c.show() when used in a jupyter notebook.

mwshinn commented 3 years ago

Looks like the dark theme is messing with the matplotlib defaults (which should be non-transparent). This forces a white background. If this push doesn't work, it looks like it might just be the theme replacing white backgrounds in images with transparency, in which case there isn't too much we can do on this end.

syncrostone commented 3 years ago

This doesn't work unfortunately, I'm attaching two screenshots to clarify the issue -- the area behind axes labels and ticks etc is dark, but the area within the axes is white, even if nothing is plotted.

Somehow the background within the axes is being treated differently -- is there any way to leverage that to get a white background?

This is low priority since I can just work in light mode, but it would be nice to have.

Screen Shot 2021-05-30 at 9 39 55 PM Screen Shot 2021-05-30 at 9 39 08 PM

mwshinn commented 3 years ago

Two clarifications: (1) what dark mode theme are you using and/or how are you enabling it? (2) This looks like dark mode trying to force transparency. Could you right click on the image and "open in new tab" or "save image" and check if the background is transparent or not?

syncrostone commented 3 years ago

This is in Jupyter Lab with JupyterLab Dark theme. When I save image as PNG and display in preview, I can see that the background is transparent.

syncrostone commented 3 years ago

I just confirmed that this issue does not occur when using matplotlib only, see below. This seems to be fixed in jupyter for matplotlib by this PR / set of PRs and issues. I think something canvas does / doesn't do must stop this fix from being applied?

Screen Shot 2021-05-31 at 12 35 32 PM

mwshinn commented 3 years ago

Something strange is going on here. For context, CanD displays an image in Jupyter by first saving it (using c.save()), and then pointing Jupyter to the saved file location. It is unlikely that it is replacing white with transparency in the displayed image, since your previous plot showed a lack of transparency inside the axes.

What this means is that all plots, not just the saved plots, will be changed by dark mode. So that means if you run a script which saves (but never displays) a plot, that saved plot will be different if you ran your script in Dark Mode (or Jupyter Lab) than if you ran it on the terminal. If true, this is rather alarming.

I also can't seem to reproduce. I installed the latest jupyterlab 3.0.16. Can you reproduce in the latest version of jupyterlab with the simple script I used here?

Screenshot from 2021-06-02 22-34-27

import cand
c = cand.Canvas(4,4,"in")
c.add_axis("name", cand.Point(.2, .2), cand.Point(.8, .8))
c.show()
syncrostone commented 3 years ago
Screen Shot 2021-06-03 at 1 58 07 PM

No, I can't reproduce it that way, I get the same output as you do.

The breaking line seems to be 'from matplotlib import pyplot as plt'
If I add that line to your simple example I get the same behavior I am seeing.

I have to import pyplot to set this: plt.rcParams.update({ 'text.usetex': True, 'text.latex.preamble': r'\usepackage{bm}' })

Is there any way around one of these issues?

mwshinn commented 3 years ago

Well for the latter issue, you can use c.use_latex(). I don't think there is any documentation on it now.

For the former issue, I can reproduce now, and can reproduce in normal jupyter notebooks too. I will look into this and also add documentation for the latex command.

syncrostone commented 3 years ago

Ok, I tried use_latex and run into a different issue (#12)

mwshinn commented 3 years ago

It turns out the issue was that matplotlib inline mode in jupyter was automatically changing the background to transparent. See: https://stackoverflow.com/questions/42656668/matplotlibrc-rcparams-modified-for-jupyter-inline-plots

This fix sets the background to zero transparency if it is detected to be saving from jupyter.