How to globally change xticks label relplot in Seaborn

2024/11/15 12:01:59

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.

enter image description here

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

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 snstips = 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 labelsax.set_xticks(ticks=value_tick) # set new labelsax.set_xticklabels(fontsize=8, rotation=45, labels=header_name)plt.show ()

OutputImage

https://en.xdnf.cn/q/119319.html

Related Q&A

How to solve MatplotlibDeprecationWarning: scipy.stats.norm.pdf warning?

I am using matplotlib in my Python code. I got following warning: xxx.py:88: MatplotlibDeprecationWarning: scipy.stats.norm.pdfy = 100 * mlab.normpdf(bin_middles, mu, sigma)*bin_width I was wondering…

time data 42:53.700 does not match format %H:%M:%S.%f (match)

I am trying to convert a column in string format to DateTime format, However, I am getting the following error, could somebody please help? The error:time data 42:53.700 does not match format %H:%M:%S…

How can I import .py file? [duplicate]

This question already has answers here:Adding a directory to sys.path with pathlib(4 answers)Closed last year.Below is my code: from pathlib import Path import os import sys sys.path.insert(0, os.path.…

python: convenient way to create list of strings of repeated elements

How can I create a list like this:["a","a","a",... (repeating "a" a hundred times") "b","b","b",(repeating "b" a hun…

An accurate python sleep function

Ive tried time.sleep(), but its accuracy is total garbage. Consider this loop, for instance:for i in range(10000000):print(i)sleep(.334)Watch the numbers it prints. If its anything like my computer, it…

How to map python dictionary key values to each other?

Suppose we have two dictionaries as below: dict_a_to_b = {2:4, 6:9, 9:3} dict_a_to_c = {2: 0.1, 6:0.2, 9: 0.8}How to map these two dictionaries to make dict_c_to_b in python? dict_c_to_b = {0.1:4, 0.2…

Installing Keyrock on Fiware in my Virtual Machine

I want install keyrock and I follow these steps, but in the step three, when I write in the console:sudo python tools/install_venv.pyConsole shows me the next fail:Could you help me, please?

Cartopy error when attempting to plot rivers

When attempting to use Cartopy to plot rivers, Im getting a URL error. Im not even sure the rivers feature will plot what I want...Im attempting to get the Galveston Ship Channel to show on my map. Her…

Have the address of the client in python

My request is : I have my web pages created in python (wherein there is html code), each pages has a button to go to the next page. Is it possible to get the address of the client when we submit an htm…

try/except issue in Python

I ran the following code as a practice purpose from one of the Python book and I want the output as shown in the URL provided below. So, when I am running the first activity in the question (from book …