Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add x-axis labels to every plot in a seaborn figure-level plot

I'm using Seaborn to make a Factor Plot.

In total, I have 4 'sub plots' (and use col_wrap =2, so I have 2 rows, each containing 2 sub plots). Only the 2 sub plots at the very bottom of the grid have x-axis labels (which I believe is the default).

Is it possible to configure the Factor Plot such that each of the 4 plots has x-axis labels? (I couldn't find this option in the Documentation or on StackOverflow)

UPDATE:

Here is the code (which generates 4 time series plots on a Factor Grid):

The dataframe (df) looks as follows:

Company     Date        Value
ABC         08/21/16    500
ABC         08/22/16    600
ABC         08/23/16    650
DEF         08/21/16    625
DEF         08/22/16    675
DEF         08/23/16    680
GHI         08/21/16    500
GHI         08/22/16    600
GHI         08/23/16    650
JKL         08/21/16    625
JKL         08/22/16    675
JKL         08/23/16    680


import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns 

df = pd.read_csv(the_file_name.csv)

g = sns.factorplot(data=df,
                   x='Date',
                   y='Value',
                   col='Company',
                   col_wrap=2,
                   sharey=False)
g.set_xlabels('')
g.set_ylabels('product count')
g.set_xticklabels(rotation=45)
plt.show()

You'll notice that the x-axis dates are shown on the bottom 2 plots. I'd like for the x-axis dates to be shown on the top 2 plots as well.

Thanks!

like image 644
equanimity Avatar asked Sep 06 '16 15:09

equanimity


People also ask

How do I change my x-axis name in Seaborn?

We make use of the set_title(), set_xlabel(), and set_ylabel() functions to change axis labels and set the title for a plot.

How do you add axis labels to a plot in Python?

Use the xlabel() method in matplotlib to add a label to the plot's x-axis.


1 Answers

I'm not sure of a way to do this in seaborn, but you can play with the matplotlib Axes instances to do this.

For example, here we'll use setp to set the xticklabels to visible for all axes in the FacetGrid (g). Note that I also set the rotation here too, since seaborn would not rotate the ticklabels on the upper row.

Finally, I have increased the space between subplots to allow room for the tick labels using subplots_adjust.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns 
from io import StringIO

the_file_name = StringIO(u"""
Company,Date,Value
ABC,08/21/16,500
ABC,08/22/16,600
ABC,08/23/16,650
DEF,08/21/16,625
DEF,08/22/16,675
DEF,08/23/16,680
GHI,08/21/16,500
GHI,08/22/16,600
GHI,08/23/16,650
JKL,08/21/16,625
JKL,08/22/16,675
JKL,08/23/16,680
""")

df = pd.read_csv(the_file_name)

g = sns.factorplot(data=df,
                   x='Date',
                   y='Value',
                   col='Company',
                   col_wrap=2,
                   sharey=False)
g.set_xlabels('')
g.set_ylabels('product count')

for ax in g.axes:
    plt.setp(ax.get_xticklabels(), visible=True, rotation=45)

plt.subplots_adjust(hspace=0.3)

plt.show()

enter image description here

like image 64
tmdavison Avatar answered Oct 22 '22 06:10

tmdavison