Skip to content Skip to sidebar Skip to footer

Figure And Subplots Tick Labels Overlapping

I am trying to put four subplots on a figure. The things that I want are: 1- The figure introduces its own x and y labels and I don't want it that way. 2- I would like to know if i

Solution 1:

I did some reading and solved it as follows.

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

linestyle = ["-s", "-x", "-+", "o-"]
plot_lines = ["A", "B", "C", "D"]
X=[4,8,16,32,64,128,256,512,1024]
plot_title=['256MB','128MB','64MB','16MB','8MB', '4MB']

file_locn = ''r'C:\Users\me\Desktop\output.xlsx'''
df = pd.read_excel(file_locn, sheet_name='1', header=[0, 1])
df_256 = df.xs(256, axis=1, level=0)
df_128 = df.xs(128, axis=1, level=0)
df_64 = df.xs(64, axis=1, level=0)
df_32 = df.xs(32, axis=1, level=0)
df_16 = df.xs(64, axis=1, level=0)
df_8 = df.xs(32, axis=1, level=0)
df_4 = df.xs(4, axis=1, level=0)

nrow=2
ncol=3
df_list = [df_256, df_128, df_64, df_16, df_8, df_4]    
fig, axes = plt.subplots(nrow, ncol, sharex=True, sharey=True)
# plot counter
count=0
for c in range(ncol):
    df_list[count].set_axis('X')

plt.xscale('symlog',base=2)

count=0
axes[0,0].set_ylabel('Y-Axis label')
axes[1,0].set_ylabel('Y-Axis label')
axes[1,0].set_xlabel('X-Axis label')
axes[1,1].set_xlabel('X-Axis label')

for r in range(nrow):
    for c in range(ncol):
        df_list[count].set_index('X').plot(style=linestyle,ax=axes[r,c], legend=False)
        axes[r,c].set_title(plot_title[count])
        axes[r,c].set_xlim(4,1024)
        count+=1

lines, labels = fig.axes[-1].get_legend_handles_labels()    
fig.legend(lines, labels, loc='upper center',ncol=4)

plt.show()

Post a Comment for "Figure And Subplots Tick Labels Overlapping"