Search code examples
pythonpandasmatplotlibseabornaxes

How to force order on X-axis values with Seaborn?


I am plotting the performance of certain models that are loaded from a dataframe. I am doing this using Seaborn. However, the order of the values on the X-axis depends on the order on which one selects the models. For example, in the image below one first selects the model PLD_level_6 and then model PLD_level_5.3:

enter image description here

As you can see, the values on the X-axis first start with v17 up until v19, and then they start with v13 up until v16. Of course, one would like the values on the X-axis to go in chronological order (i.e. from v13 up until v19). This is indeed the case if one selects the model PLD_level_5.3 first and then the model PLD_level_6, as seen below:

enter image description here

The reason this happens is because there exists no data for PLD_level_6 w.r.t. v13, v14, v15 and v16. Is there a way to force the fixed order of v13,v14,v15,v16,v17,v18,v19 on the X-axis independent of missing data?

This is the snippet of my code that I use for the plotting:

    if (number_of_metrics ==1):
        g = sns.lineplot(x="VERSION", y=metrics1[0], hue="FULL_MODEL_ID", data = v_model_results, ax=axes[0, 0])
        box = g.get_position()
        g.set_position([box.x0, box.y0, box.width * 0.85, box.height])
        g.legend(loc='lower right', bbox_to_anchor=(0.8, 1.2), ncol=1)
    if (number_of_metrics > 1):
        g = sns.lineplot(x="VERSION", y=metrics1[0], hue="FULL_MODEL_ID", data = v_model_results, ax=axes[0, 0])
        box = g.get_position()
        g.set_position([box.x0, box.y0, box.width * 0.85, box.height])
        g.legend(loc='lower right', bbox_to_anchor=(0.8, 1.5), ncol=1)
        for metric in metrics1[1:]:
            index = metrics1.index(metric)
            sns.lineplot(x="VERSION", y=metric, hue="FULL_MODEL_ID", data = v_model_results, ax=axes[index, 0], legend=False)
        axes[0,0].set_title(f"First set of models: {cell_line1}-{conc1}")
        plt.tight_layout()

Solution

  • Yes, give your x variable a Categorical dtype with all categories you want to appear in the order that you desire.