Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control xaxis tick mark size on all subplots

I have the below code, which works to a point. However, it only changes the size of the xticks for one of the subplots. How can I change it to change the size for all of them?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

input_file = 'CSP.csv'
output_file = 'sub_plots.png'
df = pd.read_csv(input_file, header = 0)
classes = ['SD','SC','FIR','MD','UA','LB']

f, axarr = plt.subplots(2, 3)
f.tight_layout()

i=0
for r in range(2):
    for c in range(3):
        axarr[r][c].hist(df.CSP_change[(df.Class==classes[i])].values,10,rwidth=1, alpha=0.3)
        axarr[r][c].set_title(classes[i])
        axarr[r][c].spines['top'].set_visible(False)
        axarr[r][c].spines['right'].set_visible(False)
        axarr[r][c].spines['bottom'].set_visible(True)
        axarr[r][c].spines['left'].set_visible(True)
        axarr[r][c].get_xaxis().tick_bottom()
        axarr[r][c].get_yaxis().tick_left()
        plt.xticks(fontsize=6)
        i += 1

#save
plt.savefig(output_file)

histogram

I tried changing plt to axarr[r][c] but it gave me an error message about not having attribute xticks.

like image 852
TaxpayersMoney Avatar asked Mar 01 '16 11:03

TaxpayersMoney


1 Answers

plt.xticks only acts on the final subplot axes created. You want to set it on all the subplots. You have two options:

1) You can set the tick label size for each axes using the tick_params property of the axes. Use this line instead of the plt.xticks line you currently have:

axarr[r][c].tick_params(axis='x',labelsize=6)

2) You can set it globally for all subplots using rcParams (as suggested by @DavidG). Put this line before you create the figure and axes:

plt.rcParams['xtick.labelsize'] = 6
like image 171
tmdavison Avatar answered Oct 21 '22 16:10

tmdavison