1

I have 4 columns in a dataframe: Country, City, Year and Temperature. I want to make plot as I did with this command:

g =sns.FacetGrid(grouped,col='Country',hue='City', col_wrap=3)
g = g.map(plt.plot,"year", "AverageTemperatureCelsius")

Now I can add a legend with:

g.add_legend(title='$\\bf{City}$',handlelength=1,prop={'size': 16})

Resulting in a plot like this:

enter image description here

But I want legend to be countries, meaning I would like the labels to be:

label=['Brazil', 'France',    'Japan',  'New Zealand', 'Poland', 'South Africa','Sweden', 'Ukraine']

And at each subplot there would be one colour, how can I achieve that?

The result I am after:

enter image description here

szczor1
  • 191
  • 1
  • 2
  • 12

1 Answers1

1

Setting hue=None should both make all of the subplots the same color, and change the legend to display your column data instead.

The 'hue' argument selects a subset of your data to emphasize in the subplots, docs here.

Edit: if you want to keep the hue parameter, but have it plot all the same colors, you can use keyword arguments.

colors = {'colors': ['b']} 
g =sns.FacetGrid(grouped,col='Country',hue='City',hue_kws= colors, col_wrap=3)

To then fix the legend, this answer puts it really nicely.

M-Wi
  • 392
  • 2
  • 11
  • But I want the lines for each city to be separate, this is what I use hue for. If I ommit the hue I will end up with one line for country. – szczor1 Apr 16 '20 at 13:38
  • Ok, I edited for that but my change won't get the legend you want. However Seaborn legends are just Matplotlib objects, so they're customizable. I linked to an answer that should help with that. – M-Wi Apr 16 '20 at 13:53
  • Thank you for your effort, but my problem still remains, I want each subplot to have its own colour. The way you proposed every subplot has one colour. – szczor1 Apr 16 '20 at 14:03
  • If you set both 'hue' and 'col' to 'Country' it will color the subplots according to country. However, this may lose the definition in the data you're after. Let me know if that doesn't work and I'll try to figure out how you need to customize the subplots directly. – M-Wi Apr 16 '20 at 14:10
  • Yes, this doesn't help because the result forgets about the cities then. Meaning I get one line at each subplot. I will add what the result should look like. – szczor1 Apr 16 '20 at 14:11