简体   繁体   中英

Matplot legends are printing twice

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()

这是输出

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()

在此处输入图像描述

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

plt.legend(numpoints=None)

Let me know if it helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM