繁体   English   中英

自动更改 subplot_adjust 中的左右参数?

[英]Changing the left and right parameters in subplot_adjust automatically?

我正在使用示例处理此代码:

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import seaborn as sns
sns.set()
%matplotlib notebook
#plt.style.use('classic')



Class = ['test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7']
Lens = [111, 80, 114, 59, 109.0491744186047, 117, 120]
Nums = [124, 28, 22, 41, 85, 33, 156]
data = pd.DataFrame(data=zip(Class,Lens,Nums),columns=['Class','Lens','Nums'])
data.set_index('Class', inplace=True)

font_color = '#525252'
hfont = {'fontname':'Calibri'}
#facecolor = '#eaeaf2'
color_red = '#fd625e'
color_blue = '#01b8aa'
index = data.index
column0 = data['Lens']
column1 = data['Nums']
title0 = "Title 1"
title1 = 'Title 2'

fig, axes = plt.subplots(figsize=(10,5), ncols=2,sharey=True)
fig.tight_layout()
fig.canvas.set_window_title('test') # Adds new title to the window

axes[0].barh(index, column0, align='center', color=color_red, zorder=10)
axes[0].set_title(title0, fontsize=18, pad=15, color=color_red, **hfont)
axes[1].barh(index, column1, align='center', color=color_blue, zorder=10)
axes[1].set_title(title1, fontsize=18, pad=15, color=color_blue, **hfont)


# If you have positive numbers and want to invert the x-axis of the left plot
axes[0].invert_xaxis() 

# To show data from highest to lowest
plt.gca().invert_yaxis()

axes[0].grid(False)
axes[1].grid(False)
axes[0].set_facecolor('white')
axes[1].set_facecolor('white')


axes[0].set(yticks=data.index, yticklabels=data.index)
axes[0].yaxis.tick_left()
axes[0].tick_params(axis='y', colors='white') # tick color


axes[1].set_xticklabels(['0','20', '40', '60', '80', '100', '120'])


for label in (axes[0].get_xticklabels() + axes[0].get_yticklabels()):
    label.set(fontsize=13, color=font_color, **hfont)
for label in (axes[1].get_xticklabels() + axes[1].get_yticklabels()):
    label.set(fontsize=13, color=font_color, **hfont)
    
plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1, left=0.18, right=0.95)



#axes[0].update_xaxes(showline=True, linewidth=2, linecolor='black')
#plt.update_yaxes(showline=True, linewidth=2, linecolor='black')

plt.show()

输出大致如预期:

在此处输入图片说明

但是,我可以看到右侧 x 轴上的数字比左侧的数字更紧密地聚集在一起,我想让它们均匀。

我知道这与这条线有关:

plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1, left=0.2, right=0.8)

我知道如何手动更改(即只更改左右参数)。 但是我永远无法确定我是否准确,因为我只是手动更改这些参数直到它们看起来均匀?

我想知道,如果有人可以告诉我怎么做轴均匀间隔自动,无需手动更改leftright参数?

解决此问题的一种方法是使用set_xlim函数并将其应用于子图。 代码如下所示:

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import seaborn as sns
sns.set()
#%matplotlib notebook
#plt.style.use('classic')



Class = ['test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7']
Lens = [111, 80, 114, 59, 109.0491744186047, 117, 120]
Nums = [124, 28, 22, 41, 85, 33, 156]
data = pd.DataFrame(data=zip(Class,Lens,Nums),columns=['Class','Lens','Nums'])
data.set_index('Class', inplace=True)

font_color = '#525252'
hfont = {'fontname':'Calibri'}
#facecolor = '#eaeaf2'
color_red = '#fd625e'
color_blue = '#01b8aa'
index = data.index
column0 = data['Lens']
column1 = data['Nums']
title0 = "Title 1"
title1 = 'Title 2'

fig, axes = plt.subplots(figsize=(10,5), ncols=2,sharey=True)
fig.tight_layout()
fig.canvas.set_window_title('test') # Adds new title to the window

axes[0].barh(index, column0, align='center', color=color_red, zorder=10)
axes[0].set_title(title0, fontsize=18, pad=15, color=color_red, **hfont)
axes[1].barh(index, column1, align='center', color=color_blue, zorder=10)
axes[1].set_title(title1, fontsize=18, pad=15, color=color_blue, **hfont)

print(column1)
# If you have positive numbers and want to invert the x-axis of the left plot
axes[0].invert_xaxis() 

# To show data from highest to lowest
plt.gca().invert_yaxis()

axes[0].grid(False)
axes[1].grid(False)
axes[0].set_facecolor('white')
axes[1].set_facecolor('white')


axes[0].set(yticks=data.index, yticklabels=data.index)
axes[0].yaxis.tick_left()
axes[0].tick_params(axis='y', colors='white') # tick color


#Set xlim
max_columns_value=max([max(column0),max(column1)])

axes[0].set_xlim([max_columns_value,0])
axes[1].set_xlim([0,max_columns_value])



for label in (axes[0].get_xticklabels() + axes[0].get_yticklabels()):
    label.set(fontsize=13, color=font_color, **hfont)
for label in (axes[1].get_xticklabels() + axes[1].get_yticklabels()):
    label.set(fontsize=13, color=font_color, **hfont)
    
plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1, left=0.18, right=0.95)

#axes[0].update_xaxes(showline=True, linewidth=2, linecolor='black')
#plt.update_yaxes(showline=True, linewidth=2, linecolor='black')

plt.show()

输出给出: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM