【问题标题】:figure and subplots tick labels overlapping图和子图刻度标签重叠
【发布时间】:2021-01-13 21:02:31
【问题描述】:

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

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

输出:

【问题讨论】:

  • 您使用从未使用过的 plt.subplots() 命令创建 8 个 Axes 对象,然后使用 4 个 ax1=fig.add_subplot()... 命令创建另外 4 个。将第一个subplots() 命令更改为fig=plt.figure(),你应该没问题(或者,不要调用由plt.subplots()df_128 等创建的Axes 对象,并立即覆盖下面几行中的这些句柄,而是调用它们是明智的ax1, ax2, ... 并使用下面的那些而不需要调用add_subplot 4 次)。
  • @tmdavison 我对python和pandas很陌生,对这些东西没有具体的了解。正如你所说,我改变了第一个'subplots()',但它给了我一个错误。 'TypeError: __init__() 得到了一个意外的关键字参数 'sharex''

标签: python pandas matplotlib subplot figure


【解决方案1】:

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

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

【讨论】: