Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

matplotlib - Is it possible to add x-axis labels to *every* plot in the Seaborn Factor Plot (Python)?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...