I am encountering a strange issue! I am trying to plot using groupedbar but I facing this strange issue. here is the code to generate the data and plot it:
nam = string.(repeat(1:20, outer=2))
sx = repeat(["Pre-Polarization", "Post-Polarization"], inner = 20)
c = 1:40
groupedbar(nam, c, group = sx, xlabel = "Groups", ylabel = "Scores",
title = "Scores by group and category", bar_width = 0.9,
lw = 0, framestyle = :box)
And I get the following results:
Does anybody know the reason it's happening?
The reason the X axis values look strange is the Julia is sorting the numbers as strings, not as numbers. This means, for example, that "3" > "20" in your code for nam.
To fix this you should not stringify nam before it is plotted. So use
nam = repeat(1:20, outer=2)
in the above code.
If strings are required, you can exploit the fact that space comes before numbers in lexicographical sort, such that " 3" < "10"
For example:
nam = (repeat(1:20, outer=2))
# Space sorts before numbers
nam = [ n >= 20 ? "$n" :
n >= 10 ? " $n" :
" $n"
for n in nam]
sx = repeat(["Pre-Polarization", "Post-Polarization"], inner = 20)
c = 1:40
groupedbar(nam, c, group = sx, xlabel = "Groups", ylabel = "Scores",
title = "Scores by group and category", bar_width = 0.9,
lw = 0, framestyle = :box)
The keen-eyed observer might notice a slight misalignment of the numbers now. This can be fixed by using either U+200B Zero Width Space or an U+2063 Invisible Seperator in place of the regular space, though it would make the code harder to read.
Related
I'm trying to have a plot title which contains variable values and also characters with subscripts, however when I try:
title = "ηₛ = $η̂[Pa S] , μₛ = $μ̂[Pa], μₚ = $μ̂ₚ[Pa] , ηₚ = $η̂ₚ[Pa S] \n α = $α̂ , ζ = $ζ̂"
Inside the plot function, the title appears with X marks where the subscripts are. I tried to use LaTeX ```title = L" .." but then the variable values don't appear.
Is there any way to have both in the title I need?
If you want a fully working solution this is what I think you need to do, note that %$ is used for interpolation:
title = L"\eta_1 = %$(η̂[Pa, S])"
The reason is that, while some of the characters will be rendered correctly as Bill noted, not all of them will unless you use LaTeXStrings.jl.
See:
help?> LaTeXStrings.#L_str
L"..."
Creates a LaTeXString and is equivalent to latexstring(raw"..."), except that %$ can be used for interpolation.
julia> L"x = \sqrt{2}"
L"$x = \sqrt{2}$"
julia> L"x = %$(sqrt(2))"
L"$x = 1.4142135623730951$"
I am learning how to create plots with slider bars. Here is my code based off the first example of this tutorial
using Plots
gr()
using GLMakie
function plotLaneEmden(log_delta_xi=-4, n=3)
fig = Figure()
ax = Axis(fig[1, 1])
sl_x = Slider(fig[2, 1], range = 0:0.01:4.99, startvalue = 3)
sl_y = Slider(fig[1, 2], range = -6:0.01:0.1, horizontal = false, startvalue = -2)
point = lift(sl_x.value, sl_y.value) do n, log_delta_xi
Point2f(n, log_delta_xi)
end
plot(n, 1 .- log_delta_xi.^2/6, linecolor = :green, label="n = $n")
xlabel!("ξ")
ylabel!("θ")
end
plotLaneEmden()
When I run this, it gives UndefVarError: plot not defined. What am I missing here?
It looks like you are trying to mix and match Plots.jl and Makie.jl. Specifically, the example from your link is entirely for Makie (specifically, with the GLMakie backend), while the the plot function you are trying to add uses syntax specific to the Plots.jl version of plot (specifically including linecolor and label keyword arguments).
Plots.jl and Makie.jl are two separate and unrelated plotting libraries, so you have to pick one and stick with it. Since both libraries export some of the same function names, using both at once will lead to ambiguity and UndefVarErrors if not disambiguated.
The other potential problem is that it looks like you are trying to make a line plot with only a single x and y value (n and log_delta_xi are both single numbers in your code as written). If that's what you want, you'll need a scatter plot instead of a line plot; and if that's not what you want you'll need to make those variables vectors instead somehow.
Depending on what exactly you want, you might try something more along the lines of (in a new session, using only Makie and not Plots):
using GLMakie
function plotLaneEmden(log_delta_xi=-4, n=3)
fig = Figure()
ax = Axis(fig[1, 1], xlabel="ξ", ylabel="θ")
sl_x = Slider(fig[2, 1], range = 0:0.01:4.99, startvalue = n)
sl_y = Slider(fig[1, 2], range = -6:0.01:0.1, horizontal = false, startvalue = log_delta_xi)
point = lift(sl_x.value, sl_y.value) do n, log_delta_xi
Point2f(n, 1 - log_delta_xi^2/6)
end
sca = scatter!(point, color = :green, markersize = 20)
axislegend(ax, [sca], ["n = $n"])
fig
end
plotLaneEmden()
Or, below, a simple example for interactively plotting a line rather than a point:
using GLMakie
function quadraticsliders(x=-5:0.01:5)
fig = Figure()
ax = Axis(fig[1, 1], xlabel="X", ylabel="Y")
sl_a = Slider(fig[2, 1], range = -3:0.01:3, startvalue = 0.)
sl_b = Slider(fig[1, 2], range = -3:0.01:3, horizontal = false, startvalue = 0.)
points = lift(sl_a.value, sl_b.value) do a, b
Point2f.(x, a.*x.^2 .+ b.*x)
end
l = lines!(points, color = :blue)
onany((a,b)->axislegend(ax, [l], ["$(a)x² + $(b)x"]), sl_a.value, sl_b.value)
limits!(ax, minimum(x), maximum(x), -10, 10)
fig
end
quadraticsliders()
ETA: A couple examples closer to what you might be looking for
I am using substitution labels (##) with diagrammeR and Graphviz syntax. I have seen previous questions about justification of node labels such as this one when the labels are in-line text, but I am wondering how to justify node text generated from a multi-row substitution label. More specifically, for the label in the reproducible example below, I want the ‘main’ column, meaning the first and third rectangle labels, to remain centered, but multi-line node labels such as the rightmost rectangle to be left justified (the value as well as the subvalues). Since I specify line breaks in the substitution labels, I tried using double backslash \l instead of \n without success.
Additionally, I would like to bold the headers (in the reproducible example, the first value, second value, and third value rows), but not bold any subvalues.
Any help would be greatly appreciated. Thank you!
library(DiagrammeR)
library(DiagrammeRsvg)
a <- 100
x <- 50
b <- 30
d <- 20
grViz("
digraph a_nice_graph {
node[fontname = Helvetica, shape = box, width = 4, fontcolor = darkslategray]
firstvalue[label = '##1']
secondvalue[label = '##2']
thirdvalue[label = '##3']
blank[label = '', width = 0.01, height = 0.01]
{ rank = same; blank secondvalue }
firstvalue -> blank [dir = none]
blank -> secondvalue[minlen = 9]
blank -> thirdvalue
}
[1]: paste0('First value (n = ', a, ')')
[2]: paste0('Second value (n = ', a-x, ')\\nSubvalue = ', b, '\\nSubvalue = ', d, '')
[3]: paste0('Third value (n = ', x, ')')
")
I am working on image processing and I need to segment colour in image into several groups.I am using k-means algorithm to do that.This time, I want to give English name to the colour which already been clustering. this is my coding:
cform = makecform('srgb2lab');
lab = applycform(a,cform);
ab = double(lab(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors=6;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', 'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
as far as I understand, cluster_center is refer to a*b (lab colour space) value. Is it correct? If so, why I get the value of cluster_center more than range value already set (-128 to 128 for lab color space).
I tried this
a = 1+3*%i;
disp("a = "+string(a))
I got a = 1+%i*3 , but what I want is a = 1. + 3.i
So is there any method in Scilab to print a complex number without the percent sign?
Similarly to Matlab, you can format the output string by including the real and imaginary parts separately.
mprintf('%g + %gi\n', real(a) , imag(a))
However, that looks pretty ugly when the imaginary part is negative. I suggest writing a formatting function:
function s = complexstring(a)
if imag(a)>=0 then
s = sprintf('%g+%gi', real(a) , imag(a))
else
s = sprintf('%g%gi', real(a) , imag(a))
end
endfunction
Examples:
disp('a = '+complexstring(1+3*%i))
disp('b = '+complexstring(1-3*%i))
Output:
a = 1+3i
b = 1-3i