Seaborn regplot stops matching legend color to line color when I include x_bins in the parameters. It works fine until I add x_bins and then the multi-colored legend loses its color differentiation.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
import pandas as pd
import seaborn as sns
data=pd.DataFrame({"VarX" : np.arange(10),
'VarY1': np.random.rand(10),
'VarY2': np.random.rand(10),
'VarY3': np.random.rand(10)})
fig = plt.figure(figsize=(10,6))
sns.regplot(x='VarX', y='VarY1', data=data, x_bins=10)
sns.regplot(x='VarX', y='VarY2', data=data, x_bins=10)
sns.regplot(x='VarX', y='VarY3', data=data, x_bins=10)
fig.legend(labels=['First','Second','Third'])
plt.show()
Seaborn has its own concept of legend which often conflicts with default matplotlib legends.
To stay in the seaborn way of thinking, you can use lmplot
for this and let it automatically produce the legend. This requires to reshape the input data a bit.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
import pandas as pd
data=pd.DataFrame({"VarX" : np.arange(10),
'VarY1': np.random.rand(10),
'VarY2': np.random.rand(10),
'VarY3': np.random.rand(10)})
df = data.set_index("VarX")
df.columns = ['First','Second','Third']
df = df.stack().rename_axis(['VarX','Ycategory']).rename('VarY').reset_index()
sns.lmplot(x="VarX", y="VarY", hue="Ycategory", data = df, x_bins=10)
plt.show()
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.