图和子图的刻度标签重叠 [英] figure and subplots tick labels overlapping

查看:189
本文介绍了图和子图的刻度标签重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个图形上放置四个子图. 我想要的东西是:

I am trying to put four subplots on a figure. The things that I want are:

1-该图引入了自己的x和y标签,但我不希望这样.

1- The figure introduces its own x and y labels and I don't want it that way.

2-我想知道在所有子图中所有的y轴标签是否可能具有相似的值

2- I would like to know if it is possible to have similar values for the y-axis labels throughout all the labels of subplots

3-我想要的实际图形可能包含高达3x3(最多9个子图)的子图.有没有一种方法可以使某种功能从每个子图的数据框中提取数据并绘制图形?

3- The actual figure I want could contains subplots as big as 3x3(upto 9 sub-figures). Is there a way to make some kind of function(s) that can extract data from the dataframe for each subplot and plot the graphs?

这是我使用的代码和输出图.

Here are the codes I use and the output figure.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
  
fig, (df_256,df_128,df_64,df_32) = plt.subplots(4, 2, sharex='col', sharey='row')
file_locn = ''r'C:\Users\me\Desktop\output.xlsx'''
df = pd.read_excel(file_locn, sheet_name='1', header=[0,1])
   
#print(df)

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)

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

ax1.set_xscale('symlog', base=2)
ax2.set_xscale('symlog', base=2)
ax3.set_xscale('symlog', base=2)
ax4.set_xscale('symlog', base=2)

ax1.set_yscale('log')
ax2.set_yscale('log')
ax3.set_yscale('log')
ax4.set_yscale('log')
    
'''print(df_256)
print(df_128)
print(df_64)
print(df_32)'''

color = ['blue', 'limegreen', '#bc15b0', 'indigo']
linestyle = ["-", ":", "--", "-."]
plot_lines = ["A", "B", "C", "D"]
df_256.set_index('X').plot( style=linestyle,ax=ax1)
df_128.set_index('X').plot(style=linestyle,ax=ax2)
df_64.set_index('X').plot( style=linestyle,ax=ax3)
df_32.set_index('X').plot( style=linestyle,ax=ax4)
 
plt.show()

输出:

推荐答案

我做了一些阅读,并按如下所示解决了问题.

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()

这篇关于图和子图的刻度标签重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆