BhishanPoudel
BhishanPoudel

Reputation: 17144

How to chain the command 'rotate xticklabels' to seaborn plot

I was learning the pandas piping method with seaborn plots: Most of the things are easily easily chained in one-liner, but I was having difficulty piping the xticklabel rotations.

How to do so?

Code:

import numpy as np
import pandas as pd

# plotting
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
names = ['mpg','cylinders', 'displacement','horsepower','weight',
         'acceleration','model_year', 'origin', 'car_name']

url = "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
df = pd.read_csv(url, sep='\s+', names=names)

Plot

g = ( df.pipe((sns.factorplot, 'data'), x='model_year', y='mpg')
)

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

Required skeleton:

( df.pipe((sns.factorplot, 'data'), x='model_year', y='mpg')
.set(xlim=(0,90), ylim=(0,80))
.set (xticklabel_rotation = 45)
)

Is this possible?

Required Image:
enter image description here

But I am getting:
enter image description here

Upvotes: 0

Views: 169

Answers (1)

jwalton
jwalton

Reputation: 5686

You were almost there. Instead of .set(xticklabel_rotation = 45) you wanted .set_xticklabels(rotation=45)

import pandas as pd

import seaborn as sns
names = ['mpg','cylinders', 'displacement','horsepower','weight',
         'acceleration','model_year', 'origin', 'car_name']

url = "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
df = pd.read_csv(url, sep='\s+', names=names)

(df.pipe((sns.factorplot, 'data'), x='model_year', y='mpg')
.set_xticklabels(rotation=45)
)

This gave me:

enter image description here

Upvotes: 1

Related Questions