TaxpayersMoney
TaxpayersMoney

Reputation: 689

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.

Upvotes: 6

Views: 5397

Answers (3)

Martin Evans
Martin Evans

Reputation: 46759

The following approach would let you set it on a per subplot basis:

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

plt.tick_params(axis='both', which='major', labelsize=6)

for rows in axarr:
    for ax_c in rows:
        ax_c.hist(df.values, 10, rwidth=1, alpha=0.3)
        ax_c.set_title(classes[i])
        ax_c.spines['top'].set_visible(False)
        ax_c.spines['right'].set_visible(False)
        ax_c.spines['bottom'].set_visible(True)
        ax_c.spines['left'].set_visible(True)
        ax_c.get_xaxis().tick_bottom()
        ax_c.get_yaxis().tick_left()
        ax_c.tick_params(labelsize=6)
        i += 1

#save
plt.savefig(output_file)

Upvotes: 1

tmdavison
tmdavison

Reputation: 69116

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

Upvotes: 5

DavidG
DavidG

Reputation: 25362

I think the easiest solution is to do:

import matplotlib as mpl

# some code here

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

label_size = 6
mpl.rcParams['xtick.labelsize'] = label_size #add these lines in 

# the rest of your code

Upvotes: 4

Related Questions