PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, June 28, 2022

[FIXED] How to remove a legend part of a seaborn facetgrid

 June 28, 2022     graph, legend, matplotlib, python, seaborn     No comments   

Issue

In Matplotlib/seaborn I create a facetgrid with the relplot command where the data attribute use for therow parameter is also used for the style attribute. This leads to a legend with two parts. One part is redundant and I want to remove this redundant part of the legend.

Here the code:

df = pd.read_csv('data.csv')
# Dataframe df has columns 'size', 'pricepersize', 'date' and 'series'

g = sns.relplot(x='size',
                y='pricepersize',
                data=df,
                kind='line',
                hue='date',
                style='series',
                row='series',
                markers=True
                )

plt.show()

And here the resulting graph grid (with the part of the legend I want to remove marked up in green):

facetgrid  with redundant legend part

How can I get rid of the "series" part in the legend, but keep the style parameter set to the same data column as the row parameter?


Solution

I guess the easiest way is to let sns create the legend (this is the default), remove it and re-generate a new one from the desired entries of the original legend.

import seaborn as sns

tips = sns.load_dataset("tips")
fg = sns.relplot(data=tips, x="total_bill", y="tip", hue="day", row="time", kind='line', style='time')

gives

enter image description here

then use

fg.legend.remove()
fg.fig.legend(handles=fg.legend.legendHandles[:5], loc=7)

to finally get

enter image description here



Answered By - Stef
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing