Plot Labelling

Hi everyone,

I’m trying to make a graph of covid-19 case numbers in 3 countries. I’m having trouble with getting the labels to cooperate. Unlike what’s shown below, I want something like blue: AUS, orange: UK, etc…


This is my code right now (‘Aus_data’, ‘UK_data’, ‘US_data’ and ‘dates’ are variables from data):

country_list = DataFrame(AUS = Aus_data, UK = UK_data, US = US_data);
p = plot()
for col in eachcol(country_list)
      plot!(dates, col,
      xticks = dates[1:5:end],
      xrotation = 45,
      label  = [names(country_list)],
      xlabel = "Date",
      ylabel = "Confirmed Cases",
      title  = "Confirmed COVID-19 Cases")

Sorry if this is a trivial question - I’m new to Julia, and thank you for your time!

When you set label = [names(country_list)] in your loop you get the full list of names back. You only want the name of one column, which you can’t extract from the column itself if you iterate over eachcol, as that only returns the coluns itself without the name.

You could iterate over for (name, col) in zip(names(country_list), eachcol(country_list)) and then use label = name

1 Like

Well the first problem I see is that you input to label, “names(country_list)” isnot indexed into, meaning that you pass all column names to the label for each loop, explaining your result. So you need to use an updating index to access the correct column-name.

But I would do this by simply calling plot(x-vals, y-vals) and use plot! for the following datasets, adding the appropriate label for each. You can then add modifications like ticks and x/y-labels with their own modifier-commands (like “xticks! ()”), or include them in the first call to “plot()”

I didn’t know that was a thing, thank you so much, I learnt something new today!

You can also plot this without a loop

dates = -4:5
country_list = DataFrame(AUS = rand(10), UK = rand(10), US = rand(10))
plot(dates, eachcol(country_list), 
     labels = hcat(names(country_list)...))