The plot command moves the graph

Hello, I’m new programming in Julia. I have two queries regarding the Plots package. (i) Is there a way to integrate the colors generated by “distinguishable_colors (10)” into the scatter below? (ii) Do you know why the graph is not centered?

Welcome to the community.
Please read the guidelines here and post a MWE by enclosing the code blocks in triple-backticks, to increase the chances of receiving feedback more swiftly.

2 Likes

Re (i), did you try color=distinguishablecolors(10)?

Like:

plot(hola[:,11], hola[:,5], seriestype=:scatter,
group=hola[:,:Region_1], color=[distinguishable_colors(10)]

It’s really hard to help you when we don’t know what code you are running or whether it does what you expect.

plot(rand(10,5); color=distinguishable_colors(5)')

produces five line plots, each with a different distinguishable color. Do you want something like that?

2 Likes

Thanks, yes it worked. I will be more careful when asking the questions.

plot(hola[:,11], hola[:,5], seriestype=:scatter, group=hola[:,:Region_1], color=distinguishable_colors(5)', legend=:topleft, markersize=8, alpha=0.6)

1 Like

Get help! created a minimal reproducible example:

using Plots

hola = Array{Union{String, Float64}}(undef, 50, 3);

hola[:,1]=["Region_$i" for i in  rand(1:4,50)];

hola[:,2:3]=10*rand(50,2);

plot(Float64.(hola[:,2]), Float64.(hola[:,3]), seriestype=:scatter, group=hola[:,1], color=distinguishable_colors(4)', legend=:outertopright, markersize=8, alpha=0.6)

reproex

Hello, I have this code that puts the name of all the countries where it corresponds, but if I only want to put the name of only a specific group (for example Region_1), what would I have to add?


using Plots, Random
hola_3 = Array{Union{String, Float64}}(undef, 50, 4);
hola_3[:,1]=["Region_$i" for i in  rand(1:4,50)];
hola_3[:,2:3]=10*rand(50,2);
hola_3[:,4].=randstring(6);

plot(Float64.(hola_3[:,2]), Float64.(hola_3[:,3]), seriestype=:scatter, group=hola_3[:,1], color=distinguishable_colors(4)', legend=:outertopright, markersize=8, alpha=0.6, series_annotations=text.(hola_3[:,4],8) )

You could use the ternary operator to set the text vector to nothing when the first column is not equal to…

But the easier way is to create the texts in a separate call to annotate!.

Why do you keep everything in a matrix, by the way? Why not a names vector, a regions vector etc. Or a DataFrame? Julia could make a lot more optimisations if it knew which columns are only Strings and which are numeric.

1 Like
using DataFrames

df = DataFrame(Region = ["Region_$i" for i in  rand(1:4,50)], 
hola_1 = 10*rand(50),
hola_2 = 10*rand(50),
hola_4 = randstring(6))

using StatsPlots # Required for the DataFrame user recipe

@df df plot(:hola_1, :hola_2, 
seriestype=:scatter, 
group=:Region, 
color=distinguishable_colors(4)', 
legend=:outertopright, markersize=8, alpha=0.6,
series_annotations=text.([:Region[i] == "Region_1" ?  :hola_4[i]  : "" for i in 1:50] ,8))

How to implement it using names vector, a regions vector?
Could you please illustrate:
only today did I know that the DataStructures and NamedArrays packages exist

1 Like

@Paolo_Jara, it is recommended to create a new post with new questions, to avoid pilling up queries with no bearing with the OP title. Thanks.

It works for me.

cantidad=50
hola_4=DataFrame(ILH=rand(cantidad), CIP=rand(cantidad),grupo=["Grupo_$i" for i in rand(1:4,cantidad)],	nombre=[randstring(6) for _ in 1:cantidad])

scatter(hola_4.ILH,hola_4.CIP, group=hola_4.grupo, color=distinguishable_colors(4)')
for i=1:cantidad
	if hola_4[i, "grupo"]=="Grupo_1"
		annotate!(hola_4[i,"ILH"],hola_4[i,"CIP"],text(hola_4[i,"nombre"],:center, 9))
	end
end
	plot!()