首页 > 解决方案 > 图和子图刻度标签重叠

问题描述

我试图在一个数字上放置四个子图。我想要的东西是:

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

2-我想知道是否可以在子图的所有标签中为 y 轴标签设置相似的值

3- 我想要的实际数字可能包含 3x3 大的子图(最多 9 个子图)。有没有办法制作某种函数,可以从每个子图的数据框中提取数据并绘制图形?

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

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

输出: 图的 x 和 y 标签不正确

标签: pythonpandasmatplotlibsubplotfigure

解决方案


我做了一些阅读并解决了如下问题。

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

推荐阅读