简体   繁体   中英

How to globally change xticks label relplot in Seaborn

I am facing an issue to globally changed the x-tick label for plot render using the relplot

The idea was to change the int to string x-tick label for both plot. The desired label is ['a','b','c','d']

However, as can be seen below, only the x label of the bottom plot was successfully changed.

在此处输入图像描述

The code to reproduced the above figure is as below.

tips = sns.load_dataset("tips")
g = sns.relplot(x="total_bill", y="tip", hue="day", col="time", data=tips, facet_kws=dict(sharex=False),col_wrap=1)
header_name=['a','b','c','d']
x_tick_interval = 15
value_tick = range ( 0, 50, x_tick_interval )
header_name_sel = [header_name[idx] for idx in  range ( 0,len(value_tick))]
# g.set_xticklabels ( fontsize=8, rotation=45, labels=header_name, step=15 )
plt.xticks ( ticks=value_tick, labels=header_name_sel, ha="right" )
plt.show ()

You can manually grab the axis as seaborn is basically wrapper around matplotlib . Refer below example.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")
g = sns.relplot(x="total_bill", y="tip", hue="day", col="time", data=tips, facet_kws=dict(sharex=False),col_wrap=1)
header_name=['a','b','c','d']
x_tick_interval = 15
value_tick = range ( 0, 50, x_tick_interval )
header_name_sel = [header_name[idx] for idx in  range ( 0,len(value_tick))]

for ax in g.axes.flat:
    labels = ax.get_xticklabels() # get x labels
    ax.set_xticks(ticks=value_tick) # set new labels
    ax.set_xticklabels(fontsize=8, rotation=45, labels=header_name)

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.

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