Search code examples
pythonjupyterhistogramaxis-labels

How to set order of x-axis on sns histplot?


The 'floorType' was originally character variable that I changed into integer (1,2,3,4,5) through Excel.

But when I loaded that cvs. file into Jupyter and draw sns histogram of this variable, the order of axis is shown as (4 -> 5 -> 3 -> 2 -> 1) but not (1 -> 2 -> 3 -> 4 -> 5) as I expected.

enter image description here

Please teach me how to set the x-axis of the histogram (sns_python) in ascending order.


Solution

  • You can use set_xticks on seaborn functions like histplot by assigning this object a name and passing it the argument as a list (e.g. ticks ordered 1 through 5):

    a = sns.histplot(data_drop['floortype'], discrete=True)
    a.set_xticks([1, 2, 3, 4, 5])