Is there a configuration file like matplotlibrc in Plots.jl? - julia

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.

Related

changing the side of bottom facet of graph in chart_Series quantmod

I would like to know if i can increase the size of the bottom pane of the graph on the plot function chart_Series()
chart_Series(x$A, TA="add_TA(x$B)")
you don't need data to know what this will look like...
It is possible to modify some aspects of chart_Series using the pars and theme objects you can optionally pass to chart_Series. But I don't know if there is a way to feed in modifying the size of the y axis in add_TA etc without directly modifying the source code for add_TA. This is what I've done before, which is a bit messy, but works ... modify the source code.
The line in add_TA you want to modify is this, which is hard coded as (approximatly line 61 of add_TA):
plot_object$add_frame(ylim = range(na.omit(xdata)),
asp = 1)
Changing that line to this (the value of asp (aspect?) is changed), will give you something like what you want:
plot_object$add_frame(ylim = range(na.omit(xdata)),
asp = 3)
This change gives:
getSymbols("AAPL")
chart_Series(AAPL["2016"])
my_add_TA(SMA(AAPL["2016", 4])) #my_add_TA is add_TA with asp line changed
If you're unsure about how to modify the source code of a package, you could follow my answer to a related question here modify chart_Series source on modifying chart_Series as one approach. Another approach is just to recompile the source code for the package with your modifications.

How can I set graphical parameters (par()) and structure options (strOptions()) in a knitr document?

I am working on a knitr-sweave document and have found that global R options like
par(lwd=3)
and
strOptions(strict.width='cut')
do not take effect in later code chunks. I can specify these options as arguments each time I plot() or str(), so it's not a huge problem. I'm just wondering if anyone has any insight into this. Should I be caching the code chunk where I set these options? I call some libraries in early code chunks and set variables in others and they all seem to be accessible "globally" (i.e. in later code chunks).
I believe I can help you with setting strOptions globally. Just set your str options as a list under options, like this:
options(str = list(strict.width = "cut"))

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.

Howto set paper size using postscript() to make it same with pdf() in R

I have already successfully plot a figure using this command:
pdf("myplot.pdf",paper="a4",height=20.0)
The figures are well placed with the proper size orientation
and size. But when I convert it to postcript with the same
parameter, it goes wrong.
postscript("myplot.eps",paper="a4",height=20.0)
What's the correct way to set the parameter in postscript()
so that it generate the same format as pdf()?
I recommend you really to use knitr. One of the motivation of the package is :
I wished for many times that if only I could use graphics devices
other than PDF and postscript; now the dream has come true in the
official R, but what I was hoping for was an option as simple as dev =
'png' or dev = 'CairoJPEG'.
For example you can create a chunck
<<dev='postscript'>>=
plot(cars) ##replace this by your code to create myplot
#

How to source file.R without output

Is it possible to source a file without printing all the charts etc (already tried with echo=F)?
In my case I call the png("filename%03d.png") device early in the script. It is not to big a hassle to comment this out - but all the charts do take a lot of time to render. (the specific file I am working with now uses base-graphics - but mostly I'll be using ggplot2 - which makes the issue somewhat more important (ggplot2 is excellent, but in the current implementation not the fastest))
Thanks
It's not a problem for ggplot2 or lattice graphics - you always have to explicitly print them when they are called in non-interactive settings (like from within a script).
Good practise for coding R means wrapping as much of your code as possible into functions. (See, e.g., Chapter 5 of the R Inferno, pdf.) If you place your plotting code inside a function, it need not be displayed when you source it. Compare the following.
File foo.r contains
plot(1:10)
When you call source('foo.r'), the plot is shown.
File bar.r contains
bar <- function() plot(1:20)
When you call source('bar.r'), the plot is not shown. You can display it at your convenience by typing bar() at the command prompt.
Perhaps this might be of some help...
"A package that provides a null graphics device; includes a vignette, "devNull", that documents how to create a new graphics device as an add on package. "
from http://developer.r-project.org/
It's not the best sounding solution, but If you might be running this script often like this, you could declare a boolean whether graphics are required (graphics_required=TRUE) and then enclose all your plot commands in if/then clauses based on your boolean, then just change the boolean to change the behaviour of the script

Resources