Plots multiple groups of series from a dataframe (seems a bug in plots?)

question
plotting
pyplot

#1

I am trying to plot multiple groups of series from a dataframe, distinguish the group by line attribute and the individual serie by colour.
The problem is that, although I define my own palette, only the first group of series complies with it:

using DataFrames, Plots, StatPlots
df = DataFrame(
  product = ["orange","orange","orange","orange","banana","banana","banana","banana"],
  year = [2010,2011,2012,2013,2010,2011,2012,2013],
  prod = [120,150,170,160,100,130,165,158],
  cons = [70,90,100,95,   80,95,110,120]
)
plotlyjs() # same with pyplot() backend..
mycolors = [:blue, :red ] # same with mycolors = [:yellow, :orange ]
fruits_plot = plot(df, :year, :prod, group=:product, linestyle = :solid, linewidth=3, linecolor=:match, color_palette = mycolors)
fruits_plot = plot!(df, :year, :cons, group=:product, linestyle = :dot, linewidth=3, linecolor=:match, color_palette = mycolors)

Produces:

As you can see, the second group of series is using the palette, but with somehow “distorted” colours.
It’s not because it’s a :dot (I did try with :solid for the second group of series too).
Am I doing something wrong, or you think it’s a bug in the Plots package?

On a side note, would it be possible to force adding a prefix to the legend, so I could write "Production of " for the first group and "Consumption of " for the second one ?..


#2

I did open a bug report on the Plots.jl package for the colour issue and a feature request for the label one… Still, feel free to comment if you believe there are workarounds to that (of if I am taking the wrong approach…) :wink:


#3

I responded to the issue (btw… don’t open an issue if you’re posting the
question here)

Likely you’re misusing palette, which determines the family of colors.
Colors are selected from the palette in an attempt to be distinguishable
from each other. Instead just set the color directly.


#4

This is not a bug.
The colors should be a row vector, as desribed here https://juliaplots.github.io/input_data/ . And you need to specify color, not color_palette. EDIT: I now see that Tom responded the same on the issue.

mycolors = [:blue :red ] # same with mycolors = [:yellow, :orange ]
fruits_plot = plot(df, :year, :prod, group=:product, linestyle = :solid, linewidth=3, linecolor=:match, color = mycolors)
fruits_plot = plot!(df, :year, :cons, group=:product, linestyle = :dot, linewidth=3, linecolor=:match, color = mycolors)