Could somebody suggest me how to initiate and end the superscript in a Gadfly plot using Julia correctly? Or if there are any other better solutions instead of using <sup></sup>, please also share them with me.
Thanks so much.
using Gadfly
ticks = [400, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
Gadfly.plot(PET, x = :Wavenumber, y = :Transmittance, Geom.line,
Coord.cartesian(xflip=true, xmin=400, xmax=4000, ymax=100, ymin=0),
Scale.x_continuous(maxticks=2000),
Guide.xticks(ticks=ticks),
Guide.xlabel("Wavenumber (cm<sup>-1</sup>)"),
Guide.ylabel("Transmittance (%)"))
In a supported text editor, you can type the superscript characters directly with tab completion. \^-1<Tab> should transform into ⁻¹ once you press <Tab>.
Copy the characters from this answer to your string: ⁻¹.
Use my function below to convert integers to their corresponding Unicode superscripts. Use the * function to join the output string with the rest of your xlabel.
"Finds and prints the appropraite unicode value for any numerical superscript."
function superscriptnumber(i::Int)
if i < 0
c = [Char(0x207B)]
else
c = []
end
for d in reverse(digits(abs(i)))
if d == 0 push!(c, Char(0x2070)) end
if d == 1 push!(c, Char(0x00B9)) end
if d == 2 push!(c, Char(0x00B2)) end
if d == 3 push!(c, Char(0x00B3)) end
if d > 3 push!(c, Char(0x2070+d)) end
end
return join(c)
end
Related
Here is the great example from StatWithJuliaBook (please find the following)
It demos how to smooth a plot of stary sky stars.png
My question is about argmax().I. According to the author, "Note the use of the trailing “.I” at the end of each argmax, which extracts the values of the co-ordinates in column-major."
What does it mean? Is there other parameter? I can't find any description in the document.
According to author, it seems to be the position of column-wise maxmum value, yet when I tried argmax(gImg, dims=2), the result is different.
#julia> yOriginal, xOriginal = argmax(gImg).I
#(192, 168)
#julia> yy, xx = argmax(gImg, dims = 2)
#400×1 Matrix{CartesianIndex{2}}:
# CartesianIndex(1, 187)
# CartesianIndex(2, 229)
⋮
# CartesianIndex(399, 207)
# CartesianIndex(400, 285)
#julia> yy, xx
#(CartesianIndex(1, 187), CartesianIndex(2, 229))
Please advise.
using Plots, Images; pyplot()
img = load("stars.png")
gImg = red.(img)*0.299 + green.(img)*0.587 + blue.(img)*0.114
rows, cols = size(img)
println("Highest intensity pixel: ", findmax(gImg))
function boxBlur(image,x,y,d)
if x<=d || y<=d || x>=cols-d || y>=rows-d
return image[x,y]
else
total = 0.0
for xi = x-d:x+d
for yi = y-d:y+d
total += image[xi,yi]
end
end
return total/((2d+1)^2)
end
end
blurImg = [boxBlur(gImg,x,y,5) for x in 1:cols, y in 1:rows]
yOriginal, xOriginal = argmax(gImg).I
yBoxBlur, xBoxBlur = argmax(blurImg).I
p1 = heatmap(gImg, c=:Greys, yflip=true)
p1 = scatter!((xOriginal, yOriginal), ms=60, ma=0, msw=4, msc=:red)
p2 = heatmap(blurImg, c=:Greys, yflip=true)
p2 = scatter!((xBoxBlur, yBoxBlur), ms=60, ma=0, msw=4, msc=:red)
plot(p1, p2, size=(800, 400), ratio=:equal, xlims=(0,cols), ylims=(0,rows),
colorbar_entry=false, border=:none, legend=:none)
I is a field in an object of type CartesianIndex which is returned by argmax when its argument has more than 1 dimension.
If in doubt always try using dump.
Please consider the code below:
julia> arr = rand(4,4)
4×4 Matrix{Float64}:
0.971271 0.0350186 0.20805 0.284678
0.348161 0.19649 0.30343 0.291894
0.385583 0.990593 0.216894 0.814146
0.283823 0.750008 0.266643 0.473104
julia> el = argmax(arr)
CartesianIndex(3, 2)
julia> dump(el)
CartesianIndex{2}
I: Tuple{Int64, Int64}
1: Int64 3
2: Int64 2
However, getting CartesianIndex object data via its internal structure is not very elegant. The nice Julian way to do it is to use the appropriate method:
julia> Tuple(el)
(3, 2)
Or just access the indices directly:
julia> el[1], el[2]
(3, 2)
I'm struggling with setting a y(x) condition that varies with x range. As an example below, the code wants to plot y=x between x=0 and x=5.1; otherwise y=2x.
Upon compilation, the code spits out the following: Expression must be a scalar or 1 element array in this context:
In other words don't know how to assign an array variable 'x' into if statement.
Thank you all for your help in advance.
PRO test
x = findgen(101.0,start=0)/10.0 ; 0.0 start, 10.0 end increment of 0.1
print,x
if x lt 5.1 then begin
y = 1.0 * x ;
endif else begin
y = 2.0* x
endelse
graph1=plot(x,y,thick=2,NAME=first,/CURRENT, $
linestyle = 0, ytitle=' y',xtitle='x' ) ; O
END
The problem is the test in your IF statement. Use WHERE instead to do something like the following.
y = x ;; need to initialize variable
low = WHERE(x lt 5.1,lw,COMPLEMENT=upp,NCOMPLEMENT=up)
IF (lw[0] GT 0) THEN y[low] = x[low] ;; technically don't need this line
IF (up[0] GT 0) THEN y[upp] = 2e0*x[upp]
I was expecting that the following code would populate E with random 1's and 0's, but that does not happen. I cannot figure out why.
Pkg.add("StatsBase")
using StatsBase
function randomSample(items,weights)
sample(items, Weights(weights))
end
n = 10
periods = 100
p = [ones(n,periods)*0.5]
E = fill(NaN, (n,periods))
for i in 1:periods
for ii in 1:n
E(ii,i) = randomSample([1 0],[(p(ii,i)), 1 - p(ii,i)])
end
end
E
The statement:
E(ii,i) = randomSample([1 0],[(p(ii,i)), 1 - p(ii,i)])
defines a local function E and is not an assignment operation to a matrix E. Use
E[ii,i] = randomSample([1, 0],[p[ii,i], 1 - p[ii,i]])
(I have fixed additional errors in your code so please check out the differences)
and for it to run you should also write:
p = ones(n,periods)*0.5
Hey so I know that due to the Unicode support in Julia one, may write for instance the letter a with the subscript 1 by typing a\_1<TAB>. Now, what if I wanted to do something like the following:
for i in [1 2 3]
println("a\_i")
end
and have the output be written as
a₁
a₂
a₃
How would I go about this without writing out all the possible subscripts myself?
You could do this (at least in version 0.6):
ltx = Base.REPLCompletions.latex_symbols
for i in 1:3
println("a$(ltx["\\_$i"])")
end
Bogumił Kamiński's answer seems the neatest, but I needed to reverse the order to get the correct string for two-digit numbers:
subscript(i::Integer) = i<0 ? error("$i is negative") : join('₀'+d for d in reverse(digits(i)))
for i=7:13 println("a"*subscript(i)) end
Building off the other answers here, I wrote a set of functions to allow for negative numbers and to work with the more complex superscript case.
function subscriptnumber(i::Int)
if i < 0
c = [Char(0x208B)]
else
c = []
end
for d in reverse(digits(abs(i)))
push!(c, Char(0x2080+d))
end
return join(c)
end
function superscriptnumber(i::Int)
if i < 0
c = [Char(0x207B)]
else
c = []
end
for d in reverse(digits(abs(i)))
if d == 0 push!(c, Char(0x2070)) end
if d == 1 push!(c, Char(0x00B9)) end
if d == 2 push!(c, Char(0x00B2)) end
if d == 3 push!(c, Char(0x00B3)) end
if d > 3 push!(c, Char(0x2070+d)) end
end
return join(c)
end
julia> for i in [1 -2 39]
println("a"*superscriptnumber(i))
println("a"*subscriptnumber(i))
end
a¹
a₁
a⁻²
a₋₂
a³⁹
a₃₉
So I have a simple example of what I want to do:
restart;
assume(can, real);
f := {g = x+can*x*y, t = x+x*y};
assign(f[1]); g;
can := 2;
plot3d(g, x = 0 .. 100, y = 0 .. 100);
while this works:
restart;
f := {g = x+can*x*y, t = x+x*y};
assign(f[1]);
can := 2;
plot3d(g, x = 0 .. 100, y = 0 .. 100);
But that assumptions are really important for my real life case (for some optimisations with complex numbers) so I cant just leve can not preassumed.
Why it plots nothuing for me and how to make it plot?
The expression (or procedure) to be plotted must evaluate to a numeric, floating-point quantity. And so, for your expression g, the name can must have a specific numeric value at the time any plot of g is generated.
But you can produce a sequence of 3D plots, for various values of can, and display them. You can display them all at once, overlaid. Or you can display them in an animated sequence. And you can color or shade them each differently, to give a visual cue that can is changing and different for each.
restart;
f := {g = x+can*x*y, t = x+x*y};
eval(g,f);
N:=50:
Pseq := seq(
plot3d(eval(g,f),
x=0..10,y=0..10,
color=RGB(0.5,0.5,can/(2*N)),
transparency=0.5*(can/(N+1))),
can=1 .. N):
plots:-display(Pseq, axes=box);
plots:-display([Pseq],insequence=true,axes=box);
By the way, you don't have to assign to g just for the sake of using the equation for g that appears inside f. Doing that assignment (using assign, say, like you did) makes it more awkward for you subsequently to create other equations in terms of the pure name g unless you first unassign the name g. Some people find it easier to not make the assignment to g at all for such tasks, and to simply use eval as I've done above.
Now on to your deeper problem. You create an expression containing a local, assumed name. and then later on you want to use the same expression but with the global, unassumed version of that name. You can create the expression, with it containing the global, unassumed name instead of the local, assumed name, buy performing a substitution.
restart;
assume(can, real);
f := {g = x+can*x*y, t = x+x*y};
{g = x + can~ x y, t = x + x y}
assign(f[1]);
g;
x + can~ x y
can := 2:
g;
x + can~ x y
# This fails, because g contains the local name can~
plot3d(g, x=0..100, y=0..100);
# A procedure to make the desired substitution
revert:=proc(nm::name)
local len, snm;
snm:=convert(nm,string);
len:=length(snm);
if snm[-1]="~" then
return parse(snm[1..-2]);
else return parse(nm);
end if;
end proc:
# This is the version of the expression, but with global name can
subsindets(g,`local`,revert);
x + can x y
# This should work
plot3d(subsindets(g,`local`,revert),
x=0..100,y=0..100);