I just started using ipython, and I'm creating figures such as:
fig, axes = plt.subplots()
xs = range(0,100)
axes.plot(xs, [x*x for x in xs], 'r')
I know that the figures can be rendered as svgs, see here. Unfortunately, the figures are always rendered as a rasterized image. The rasterized images become very ugly when I'm using the notebook's zoom feature. Is there a way to change this behavior, such that figures are displayed as svg by default?
The magic I was looking for:
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
Alternatively you might still want to show png but save a figure into a file:
plt.savefig(fig_filename, format='svg')
You can change the default figure format in the ipython profile configuration files. What I did was create a configuration profile especially for the notebook server, using:
ipython profile create nbserver
At the command line. This creates a whole bunch of files under ~/.ipython/profile_nbserver which have example lines for almost every setting you could want to change (it might be somewhere such as ~/.config/ipython instead depending on your OS, not sure about where it would be under windows). You need to look in the file ipython_notebook_config.py. You should then add the the line:
c.InlineBackend.figure_formats = ['svg']
Note that this only applied to IPython 3.x, and that you can also specify additional formats as per #HarrySchreiner's comment. For IPython 2.x, you should set c.InlineBackEnd.figure_format='svg'. To use this profile you should start the notebook with
ipython notebook --profile=nbserver
If this is too much trouble then don't give a profile name when running create, and modify the default profile instead.
Also, you may want to have the line
c.IPKernelApp.matplotlib = 'inline'
so that each notebook will automatically start with the matplotlib inline backend used.
Originally I also wanted to use the svg backend instead of png to enable zooming etc. However, I found that certain plots, such as pcolor with a large number of points can just kill my browser when using the svg backend. So I find it easier to use png, and just use the xlim and ylim commands to zoom in manually if I need to.
Also, you should definitely tweak the line c.InlineBackend.rc to set more reasonable defaults for the figure size and the fonts used.
edit
Current recommended best practice is not to use pylab, but to explicitly import matplotlib and numpy instead, so I modified my answer to stop encouraging this. See this post for the reasons why:
http://carreau.github.io/posts/10-No-PyLab-Thanks.html
Also, if svg rendering is too slow for particular plot elements (such as pcolor or plot_surface), you can pass the option rasterized=True to these plot commands. This means that those particular parts of the plot will have fast pixel based rendering, but all the other plot elements will be nicely vectorized.
Related
I'm in the process of learning R (I'm still a newcomer on SO as well). Being very used to using Visual Studio Code, I decided to choose that over RStudio or RStudio Cloud.
One of the best parts of RStudio was that plots automatically resized/reshaped themselves if we resized the right pane. Moreover, in the tutorials I watched, plots involving map data automatically rendered in the correct aspect ratio (as seen on physical maps).
I replicated the code to make my own plot of the world map. Unfortunately it rendered as a square shape, and resizing the right pane does not affect its shape:
enter image description here
Am I missing any commonly used VSC extensions which can make plots resizeable like in RStudio? (I've installed only the most downloaded extension for R, by Yuki Ueda)
If not, can I modify my code to specify the exact dimensions I need the plot to have?
Thanks in advance!
You can add robust plot viewing options for R in VSCode by installing the httpgd package and then amending your JSON settings.
First, install httpgd via the R console:
install.packages("httpgd")
Then, open your JSON settings in VSCode by opening the Command Palette (either via Ctrl+Shift+P or via View > Command Palette) and then searching for "Preferences: Open Settings (JSON)", where you'll insert the following:
"r.plot.useHttpgd": true
You can add a comma after 'true' if needed (i.e. if there are other lines below that in your JSON settings).
Following that, restart VSCode and launch the R terminal inside it, and plot something using either ggplot2 or base R graphics:
plot(mtcars$mgp, mtcars$wt)
A plot viewer should then pop up in VSCode. You can either manually resize the window there, zoom in or out, or, my preference (particularly if I'm cycling through a series of already-plotted graphs), open it in an external browser where it'll automatically adjust to however you resize the window. After that, you can save or copy the image as needed.
You can specify the dimensions when you save the file. I usually use (approximately) the golden ratio:
ggsave("./earth.png", width = 16, height = 10)
The ggsave function reference explains how you can change units - options are c("in", "cm", "mm", "px").
I try to unify my figures formats across different files. I do not want to use PyPlot.jl.
Is there a configuration file as matplotlibrc in Python? Or some other alternative way that can make me to write these format into a file without changing much about my figures code.
If I understand your question correctly (I haven't used matplotlib in ages, so not sure what the matplotlibrc file exactly does) you want to specify default settings for your plots in Plots.jl.
When using Plots in a running Julia session, you can use the default function:
julia> default(color = "red", linewidth = 10)
sets default arguments for subsequent plot commands.
If you want these defaults to be automatically available in every Julia session you can create a startup file at ~/.julia/config/startup.jl and set an environment variable there:
PLOTS_DEFAULTS = Dict(:color=> 10, :linewidth => 2)
will have the same effect as the call to default above, but will happen automatically once you start Julia.
This is discussed in the docs here.
I'm wondering is it possible to customise R help files so that certain text is colour coded and easier to read. rdoc already does this except that it sends the output to the console. I would instead, like it to be sent to the help panel (i'm using Rstudio). Is there any workaround for this?
If we run ?lm normally, we can see the usual help file in the help panel on the right below but when you do it again after using rdoc in Rstudio we get the help file colour coded which is great but its sent to the console output (left side). Ideally, we would like it to remain on display in the help panel as we are running code. The way it is now - it disappears the minute you run something.
?lm
#devtools::install_github("mdequeljoe/rdoc")
library(rdoc)
options(rdoc.by_section = FALSE)
rdoc(lm)
I want to put the code into my .rprofile similar to #csgillespie .rprofile. Note, if you follow his code you can use ?lm instead of having to call rdoc(lm) directly to produce the colour coded console output.
I have a feeling this can't be done easily (if at all?) but interested to hear any suggestions.
Thanks
This is possible, but a little involved. You'll need your own css file defined to do it, though it would be possible to create a function that writes appropriate css.
As a proof of concept, I have used a copy of the "R.css" file defined inside every package's "/html" folder, and just changed the h2 color to red, with the file saved locally as "my.css".
Anyway, once you have the css file, this function will show the appropriate help file with the appropriate styling in your R viewer window:
help_css <- function(func, css_file)
{
pack <- sub("^.*:(.*)>$", "\\1", capture.output(environment(func)))
func <- deparse(substitute(func))
x <- readLines(paste0(find.package(pack), "/html/", func, ".html"))
x[3] <- paste("<style>",
paste(readLines(css_file), collapse = "\n"),
"</style>")
writeLines(x, file.path(tempdir(), "test.html"))
myViewer <- getOption("viewer")
myViewer(file.path(tempdir(), "test.html"))
}
So, for example, if I do:
help_css(lm, "my.css")
I get:
As of RStudio v1.2 you can style RStudio's integrated help pane by creating a custom user theme (essentially an .rstheme file).
I've given help pane styling a try in extending the rscodeio theme (without colored syntax highlighting, though). The latest CSS code is found here.
The help pane styling is currently only available in the optional Tomorrow Night Bright (rscodeio) editor theme.
To use it right away, you can either
install the current rscodeio master branch using remotes:
remotes::install_github("anthonynorth/rscodeio")
And then activating the editor theme named Tomorrow Night Bright (rscodeio) under Tools → Global Options… → Appearance → Editor theme. A first attempt of the help pane CSS code is included.
or – recommended – install my fork's interim-merge branch which contains all my latest work[1] overhauling the package, including a new apply_theme parameter to activate the desired editor theme right away:
remotes::install_github("salim-b/rscodeio#interim-merge")
rscodeio::install_themes(apply_theme = "Tomorrow Night Bright (rscodeio)")
[1]: This has also been proposed upstream a while ago (1, 2) but I haven't heard back from the author since.
The result looks as follows (example for ?pal::as_string):
My jupyter notebooks that have plotly plots do not retain the plots between sessions.
This is running on a Singularity container based on the official jupyter/datascience-notebook docker image with plotly pip installed on top.
I am using the new renderer framework with the notebook renderer.
My notebooks are trusted.
My plots show up during the session without issues.
They persist across refreshes and reloads of the same notebook, even if I restart the kernel.
They disappear either when I restart the jupyter server or sometimes when I reboot the client machine and come back with a new browser session.
The output cells persist with the correct dimensions, but they are blank.
I can see that a whole bunch of js is embedded in the notebook but it does not render in the browser.
At this point, even if I nbconvert to html, they still do not show up.
Tried with Chromium and Firefox.
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
pio.renderers.default='notebook'
then later I plot a bunch of things like:
go.Figure(go.Scattergl(x = var1, y= var2, mode='markers', marker_size=1))
and
go.Figure(go.Histogram2dContour(x = var1, y= var2))
My understanding is that I am set up to retain these figures in offline (non-running) notebooks; the js generated for the plots and the entirety of plotly.js library appears to be embedded in each notebook adding up to 10s of MBs, but they are not rendered.
Due to this issue I end up having to re-run (sometimes expensive) notebooks when all I need is to take a look at a previous plot.
As a recent matplotlib/seaborn convert I absolutely love the interactivity but this is quickly becoming a showstopper at this point.
I feel like I'm missing something.
Any advice is appreciated.
For me, the following solution worked :
in the environment you're using : install orca :
conda install -c plotly plotly-orca
at the beginning of your notebook : override default renderer as 'notebook' :
import plotly.io as pio
pio.renderers.default='notebook'
plotly graphs were persistent between sessions (ie : after restart of kernels) with these modifications.
I am learning to use Jupyter/IPython Notebook as an electronic notebook. Sometimes I need simple illustrations to go along with my calculations, e.g. arrows to represent vector quantities. That's the kind of illustration for which TikZ would be used if we were in Latex. Having tried the TikZ magic extension and failed, I wonder if there's a more native (Python) way to do this. I don't see Matplotlib as the right tool for this sort of thing (correct me if I'm wrong).
If you think TikZ magic is indeed the way to go and I should try to get it to work, then do say so. Thanks.
TikZ (prefered solution)
If you're already familiar with TikZ the respective magic is probably the best option. To use it, simply follow the installation instruction in this repo (pip install git+git://github.com/mkrphys/ipython-tikzmagic.git) and load the extension as shown in on the githib page with %load_ext tikzmagic.
I just tried with IPython 3.1 and it works fine. Of course you have to have pdflatex available.
Matplotlib
If you want to draw simple arrows matplotlib can be used as well and is, of course, more pythonic than TikZ. A really simple example based on this example could look like
import matplotlib.pyplot as plt
%matplotlib inline
plt.axis('off')
plt.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, fc='k', ec='k');
For more technical plots with lots of arrows and dimensions, I totally agree with you that matplotlib is not be preferred.
Other alternatives
There is also an asymptote magic found here. I haven't tried this yet, though.
Finally, you could use svgs either written in the notebook (hints see this question, or using Inkscape or similar and embed the resulting svg-file via the from IPython.display import SVG.