Home > Software engineering >  Matplot legends are printing twice
Matplot legends are printing twice

Time:12-23

I am writing a simple code with matplotlib/seaborn to plot the data of a sample csv file. However, when call the sns.histplot() function through a for loop, the legends of each column are displaying twice. Any help would be greatly appreciated:)

Here's the code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import matplotlib
sns.set_style('darkgrid')
df = pd.read_csv('dm_office_sales.csv')
df['salary'] = df['salary'] * 3
df['sample salary'] = df['salary'] * 2
x = df['salary']
y = df['sales']
z = df['sample salary']
fig,ax = plt.subplots()
for i in [x,y,z]:
    sns.histplot(data = i, bins=50, ax=ax, palette = 'bright',alpha=0.3, label='{}'.format(i.name))
plt.legend(numpoints=1)
plt.suptitle('Sales/Salary Histogram')
plt.show()

here's the output

CodePudding user response:

Pass just the columns in question in one step, instead of looping.

sns.histplot(data=df[['salary', 'sales', 'sample salary']], ...)

Here's a demo with the tips dataset:

tips = sns.load_dataset('tips')

fig, ax = plt.subplots()
sns.histplot(tips[['total_bill', 'tip']], bins=50, 
             ax=ax, alpha=0.3, palette='bright')
plt.show()

enter image description here

CodePudding user response:

You can try setting numpoints=None in the plt.legend() function, like this:

plt.legend(numpoints=None)

Let me know if it helped!

  • Related