john22
john22

Reputation: 395

How to make axes fontsize in subplots similar in matplotlib?

I have plotted a group of 4 box-plots with matplotlib and I have used different ways to make axes font-size and shape similar but one of the subplots has a different shape. I do not know how to fix it. I have put my result here. I used rcParams also font-size in each sub-plot but none of them were a solution to this problem. How to make all of the axis font sizes similar in terms of shape??I know that it would be better to define a reproducible question but I was not sure in which part I made a mistake that's why I have written my code here. The code is below: enter image description here

import os
import numpy as np
import matplotlib.pyplot as plt
import pylab
import matplotlib as mpl
import pandas as pd
from matplotlib import cm
from matplotlib import rcParams
fig, axs = plt.subplots(2, 2,sharex=True,sharey=True)

plt.rcParams.update({'font.size': 20})
root = r'C:\Users\Master Candidate\Desktop\New folder\Desktop\Out\NEW SCENARIO\Intersection\Beta 10\intersection'
xx=[]
percentage=[]
labels = []
gg=[]
my_list = os.listdir(root)
my_list =  [file for file in sorted(my_list) if os.path.isdir(os.path.join(root, file))]
my_list= sorted(my_list)
percetanges = []
for directory in my_list:
    CASES = [file for file in os.listdir(os.path.join(root, directory)) if file.startswith('config')]   
    if len(CASES)==0:
        continue
    CASES=sorted(CASES)    
    percentage=[]   
    for filename in CASES:        
        with open(os.path.join(root, directory,filename), "r") as file: 
            lines = file.readlines()
            x = [float(line.split()[0]) for line in lines]
            y = [float(line.split()[1]) for line in lines]
            g = np.linspace(min(y),max(y),100)
            h = min(y)+6
            t = max(y)-6
            xx=[]
            for i in range(1,len(x)):
                if (y[i] < h or y[i] > t):
                    xx.append(x[i])
            percent = len(xx)/len(y)
        percentage.append(percent)       
    labels.append(directory)    
    labels=sorted(labels)
    percetanges.append(percentage)
for i, x in enumerate(percetanges):
    axs[0, 0].boxplot(x,positions=[i],whis=0.001,widths = 0.6)
plt.xticks(np.arange(len(labels)),labels)
plt.grid()
plt.ylim((0,1))
...
the same strategy for the rest of 3 subplots

At the end of the code, I finalize the procedure with saving. I mean the effort that you see above is repeated for each subplot and I do not do any thing else

Upvotes: 1

Views: 5668

Answers (1)

r-beginners
r-beginners

Reputation: 35135

There are two ways to set the x-axis font size for multiple plots

plt.setp(ax.get_xticklabels(), fontsize=14)
ax.tick_params(axis='x', labelsize=14)

Code:

import matplotlib.pyplot as plt

def example_plot(ax):
    ax.plot([1, 2])
    plt.setp(ax.get_xticklabels(), fontsize=14)
#     ax.tick_params(axis='x', labelsize=16)
    
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)

for ax in axs.flat:
    example_plot(ax)

fig.suptitle('sub title', fontsize=16)
fig.text(0.5, 0.04, '$Cr$', ha='center', va='center', fontsize=16)
fig.text(0.06, 0.5, '$a$', ha='center', va='center', rotation='vertical', fontsize=16)

plt.show()

enter image description here

Upvotes: 2

Related Questions