简体   繁体   中英

Change colors of two y-axes with Seaborn?

My code is:

import seaborn as sns
import pandas as pd

df = pd.read_excel('data.xls','data')

sns.regplot(x='X var', y='Y var1',fit_reg=False, data=df)
ax2 = plt.twinx()
sns.regplot(x='X var', y='Y var2', ax=ax2, fit_reg=False,
            color='r',data=df)

I would like "Y var1" title to be blue and the "Y var2" title to be red if possible. Either this or a legend that indicates which scatter plot corresponds to which axes.

The solution I found was:

import seaborn as sns
import pandas as pd

df = pd.read_excel('data.xls','data')

ax1 = sns.regplot(x='X var', y='Y var1',fit_reg=False, data=df)

ax1.set_ylabel('Y var1', color='b')

ax2 = plt.twinx()

ax2.set_ylabel('Y var2', color='r')

ax3 = sns.regplot(x='X var', y='Y var2', ax=ax2, fit_reg=False,
            color='r',data=df)

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