Issue with Plotly() in Julia - julia

I am using Julia for potting.
I do the following:
img = load("/Users/xxxx/xxxx/xxxx-xxxx.png")
plot!(img)
plot!(x_coordinate_holder, y_coordinate_holder, color = :black, linewidth=0.4)
However when I add plotly() before the above code, it opens up a plot in the safari window, but it remains blank. Any ideas to fix this? The plot successfully shows up when I leave it with the default backend. My guess is that img is not supported with the plotly() backend. Can anyone confirm this?

According to the Julia Plots docs here, Only the gr(), hdf5() and pyplot() support the plotting of an image. Switching to one of those backends will resolve the issue.

Related

How to refresh plots without needing to close the previous plot?

How do I get VSCode to refresh the plot when making a new plot without needing to close the previous plot tab?
Ideally, it should open up a new tab with the new plot next to the old one.
Moreover, when using Httpgd, even tho the plot refreshes without me having to close the previous plot, it opens up in a separate window outside VSCode. How do I get it to open up as a new tab like in the gif above?
I had the exact same problem and what fixed it for me was simply, in vscode, going into:
Files > Preferences > Settings > section Extensions > scroll down and find R:
find the section "Plot: Use Httpgd" and check it, and restart Vscode.
I can't confirm but I believe it might have started after installing the httpgd package, because I had the behavior you desire before installation, and the exact same behavior you experience after httpgd installation. Of course if my solution works for you (I hope), it will work under httpgd after and not the base default VsCode/R-plot viewer as in your gif.
#coip pointed out that the steps given on this stackoverflow post to me. Downloading the httpgd package from CRAN fixed the problem for me.

How do I conveniently resize a ggplot2 plot on R+VSC?

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").

How do I change fonts of plots in R and save it to a pdf?

I am trying to change the font of my R plots using the following commands:
windowsFonts(times = windowsFont("Times New Roman"))
par(family = "times")
I am using the basic plot (no ggplot etc), and notice that the changed font does not register when I save my R plot as a pdf (but it does register when saving as an image though). Is there a simple solution to solve this issue? Thanks.
This is strange but as I was playing around I ran the following two lines of code:
install.packages("extrafont")
library(extrafont)
After running the two lines of code, I tried saving the plot as a pdf again, and it turns out that the fonts have been registered. It would be nice if someone can explain why that happened.

r plot shows up only when I type in console but not in script

I ran into a very weird problem: my R code for generating a plot only works if I type it into the console but not when I ran it inside the script (with Ctrl+Enter command)... It's the same problem with all plots (regular plots or ggplots). Also I tried it on two different computers and the same thing happened. Anyone have any idea why this is happening?
One possible reason: I installed the newest version of Rstudio on both computers so it might be an issue with the version. The exact same code worked before on an older version of rstudio...Could this be it? If so, how can I fix it?
I think I figured out what the problem was: the setting in the new version of Rstudio has a default option of outputting the plots inside the Rmarkdown script (at the very end of the script). And that's why I wasn't seeing them. You could change the setting such that it outputs in the console.
Try dev.off() to reset the graphics device.
This helps with a lot of weird graphics behaviour.
Probably too late for the original poster... However, I just ran into the same problem after installing an R update. The way I fixed it was to go to preferences, R markdown, and turn off "show inline output". For me, it was just coming out at the bottom of the chunk instead of in the plot window like I wanted. Hope that helps someone!
I just ran into this problem. I mistakenly put my plot() command inside the r markdown setup chunk. I moved it to its own code chunk and it ran as expected.

iPython Notebook svg Figures by Default

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.

Resources