Home > other >  X-axis not in order when using sns.relplot
X-axis not in order when using sns.relplot

Time:01-02

I made a line graph using sns relplot but it's not showing the x-axis in the correct order. Here is the figure:

#plot line graph
g=sns.relplot( 
           data=df_fig1,
           kind="line")
g.fig.suptitle("Exams", y=1.05)
g.set(xlabel="Months",
     ylabel="Exams (%)")
g.fig.set_size_inches(8,6)
for ax in g.axes.flat:
    ax.yaxis.set_major_locator(MultipleLocator(5))
plt.show()
plt.show()

enter image description here

The x-axis order should be Jan, feb,...oct, Nov. How can I fix the order? I already fixed the index order in the dataframe but it still doesn't show up correctly on the plot.

Here is what my data frame looks like: enter image description here

CodePudding user response:

The following code has been tested using seaborn 0.11.2. A dataframe in sns.relplot with ordered x-axis

CodePudding user response:

sns.relplot always sorts it's x-axis.
If you want to treat your x axis as categories, relplot is not the right tool.
sns.catplot has an order parameter which should fit your needs, but since it's made for categorized data, it does not have an option for a line graph.
You'll have to choose between a line graph and correctly ordered data.

  • Related