xticks' labels formatting in Julia Plots - julia

Assume my Julia code is
using Plots
t = -π:1e-4:π
y = sin.(t)
plot(t,y,lw=3,legend=false)
plot!(xticks=([-π/2],["(-\\pi)/2"]))
where I want to show the parenthesis in the numerator of xticks' label while keeping its "rational" form, i.e., I don't want the numerator and the denominator to be inline. The output of the above code looks like this
One may see that the xticks' label does not include the desired parenthesis.
Here is another code to show the above function:
using Plots
t = -π:1e-4:π
y = sin.(t)
plot(t,y,lw=3,color=2,legend=false)
plot!(xticks=([-π/2],["(-π)/2"]))
which as it can be seen, the only difference with the former is in the xticks' label (in the former it is \\pi while in the latter it is π).
Now the picture looks like this:
which does not show the label in the "rational" form, i.e., both numerator and the denominator are inline which is undesired. Is there any way to overcome this issue?

Try using LaTeXStrings:
using Plots
using LaTeXStrings
gr()
t = -π:1e-4:π
y = sin.(t)
plot(t,y,lw=3,legend=false)
plot!(xticks=([-π/2],[L"\frac{(-\pi)}{2}"]))

Related

Creating animated plots in the command line with Julia

Julia has the delightful ability to generate plots constructed from Unicode symbols which are printed directly to the command line in a very straightforward way. For example, the following code generates a Unicode plot of a sine function directly to the command line:
using Plots
unicodeplots();
x = [0:0.1:2*pi;];
y = sin.(x);
plot(x,y)
I would like to try to find a way to create an animated plot of this form directly on the command line. Ideally, I would like to generate a single plot in Unicode that is ``updated" in such a way that it appears animated.
However, although printing hundreds of distinct frames to the command line is naturally less appealing, such a solution is acceptable if it ``looks" like an animation. Another less acceptable solution is to print such Unicode plots into a gif in a way that is consistent for all platforms; attempts to do any of this involving jury-rigging #animate and #gif have largely failed, since either function cannot even print Unicode plots to a file in the Windows form of Julia.
UPDATE: Here is an example of code that generates an "animation" in the command line that is not really acceptable, which simply plots each distinct frame followed by "spacing" in the command line provided by a special Unicode character (tip provided by niczky12):
using Plots
unicodeplots();
n = 100;
x = [0:0.1:4*pi;];
for i = 1:30
y = sin.(x .+ (i/2));
plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
sleep(0.01)
println("\33[2J")
end
A slight improvement might be this:
let
print("\33[2J")
for i in 1:30
println("\33[H")
y = sin.(x .+ (i/2));
plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
sleep(0.01)
end
end

How to plot the function 4(x)^2 = ((y)^2/(1-y))?

I want to plot the function
4(x)^2 = ((y)^2/(1-y));
how can I plot this?
--> 4*(x) = ((y^2)*(1-y)^-1)^0.5;
4*(x) = ((y^2)*(1-y)^-1)^0.5;
^^
Error: syntax error, unexpected =, expecting end of file
Since Scilab 6.1.0, plotimplicit() does it:
plotimplicit "4*x^2 = y^2/(1-y)"
xgrid()
Can't do more simple. Result:
Well, you have to first create a function and for that you have to express one variable in terms of the other.
function x = f(y)
x = (((y^2)*(1-y)^-1)^0.5)/4;
endfunciton
Then you need to generate the input data (i.e, the points at which you want to evaluate the function)
ydata = linspace(1, 10)
Now you push your input point through the function to get your output points
xdata = f(ydata)
Then, you can plot the pairs of x and y using:
plot(xdata, ydata)
Or even easier, without the intermediate step of generating the output data, you can simply do:
plot(f(ydata), ydata)
BTW. I find it strange that the function you are trying to plot is x in terms of y, usually, x is the input variable, but I hope you know what you are trying to accomplish.
Reference: https://www.scilab.org/tutorials/getting-started/plotting
Take care that y must be in [-inf 1[
y=linspace(-10 ,1.00001,1000);
x = sqrt(y^2./(1-y))/4;
clf; plot(y,x),plot(y,-x)
If x is a solution -x is also solution

Include a reference in the combination of expression and paste function in R

I want to add a label with mathematical notations and a number reference to my plot. I tried the expression function and the paste function, but I failed to include what I want to write in my plot.
Here is a easy version of my question:
plot(NA,xlim = c(0, 5), ylim = c(0,5))
a = 5
At point (1,1), I want to add a label γ=5 on the plot. Here is what I tried:
text(1,1,expression(paste(gamma, "=", a)))
But the plot shows that γ=a.
I am wondering how I can include a number reference in the combination of expression and paste function.
Thank you!

ggplot2 equivalent of 'factorization or categorization' in googleVis in R

Due to static graph prepared by ggplot, we are shifting our graphs to googleVis with interactive charts. But when it comes to categorization we are facing many problems. Let me give example which will help you understand:
#dataframe
df = data.frame( x = sample(1:100), y = sample(1:100), cat = sample(c('a','b','c'), 100, replace=TRUE) )
ggplot2 provides parameter like alpha, colour, linetype, size which we can use with categories like shown below:
ggplot(df) + geom_line(aes(x = x, y = y, colour = cat))
Not just line chart, but majority of ggplot2 graphs provide categorization based on column values. Now I would like to do the same in googleVis, based on value df$cat I would like parameters to get changed or grouping of line or charts.
Note:
I have already tried dcast to make multiple columns based on category column and use those multiple columns as Y input, but that it not what I would like to do.
Can anyone help me regarding this?
Let me know if you need more information.
vrajs5 you are not alone! We struggled with this issue. In our case we wanted to fill bar charts like in ggplot. This is the solution. You need to add specifically named columns, linked to your variables, to your data table for googleVis to pick up.
In my fill example, these are called roles, but once you see my syntax you can abstract it to annotations and other cool features. Google has them all documented here (check out superheroes example!) but it was not obvious how it applied to r.
#mages has this documented on this webpage, which shows features not in demo(googleVis):
http://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html
EXAMPLE ADDING NEW DIMENSIONS TO GOOGLEVIS CHARTS
# in this case
# How do we fill a bar chart showing bars depend on another variable?
# We wanted to show C in a different fill to other assets
suppressPackageStartupMessages(library(googleVis))
library(data.table) # You can use data frames if you don't like DT
test.dt = data.table(px = c("A","B","C"), py = c(1,4,9),
"py.style" = c('silver', 'silver', 'gold'))
# Add your modifier to your chart as a new variable e.g. py1.style
test <-gvisBarChart(test.dt,
xvar = "px",
yvar = c("py", "py.style"),
options = list(legend = 'none'))
plot(test)
We have shown py.style deterministically here, but you could code it to be dependent on your categories.
The secret is myvar.googleVis_thing_youneed linking the variable myvar to the googleVis feature.
RESULT BEFORE FILL (yvar = "py")
RESULT AFTER FILL (yvar = c("py", "py.style"))
Take a look at mages examples (code also on Github) and you will have cracked the "categorization based on column values" issue.

Octave hist plot number format

I want to plot data with octaves hist() function. Unfortunately, as the numbers of the x-axis are quite large, they are displayed in scientific format, for example like 2e+007.
As this is a bit hard to read, I'd like them to be dislayed only as exponentials to the base 10, without the product or the e. So just like 10^5 for example. How can I achieve this?
When I plot data with the loglog() function, it uses the scale as I need it by default, but not for the hist().
EDIT:
To be a bit more preceise, I add some code and a picture of my plot.
NUM_SAMPLES = 10000;
% Open file.
input = fopen(filename);
x = [];
for i=[1:NUM_SAMPLES]
line = fgetl(input);
data = strsplit(line, ';');
x(end + 1) = str2num(data{1,2})/(1000);
endfor
% Close file.
fclose(input);
% Plot histogram.
figure('Position',[0,0,700,500]);
hist(x, 500);
So I just read some big numbers from my file and want to plot it with hist. Because the numbers are big, the automatically get displayed in loc scale and scientific format. Here you can see an example plot:
Unfortunately set(gca(), 'xscale', 'log'); doesn't change anything, so I think this plot is also consider log scale, but just a bad number format.
You can set the xticklabel or yticklabel manually, for example:
x = [1e7, 1e8, 1e9];
ax = gca();
set(ax, 'xticklabel', {'0','2e8','4e8','6e8','8e8','10e8'});
hist(x);
Alternatively, you can set the x axis to be in log scale:
set(ax, 'xscale', 'log');

Resources