今天我一直在努力解决matplotlib中的这个空间问题。
我正在使用带有子图的 matplotlib。
当我调用 plot_circles_in_given_axis(cirle_plot_axis,...) 方法时,它会在下面产生额外的空间。
我怎样才能最小化这个空间?
这是复制它并得到图形的小代码:
但是,如果我调用上面的语句,plot_bar_plot_in_given_axis(cirle_plot_axis),这个调用不会导致任何额外的空间。
from matplotlib import pyplot as plt
from matplotlib import gridspec
import os
import numpy as np
def plot_circles_in_given_axis(ax,
label_strings,
my_types,
xticklabels_list,
y_axis_label):
ax.set_aspect(1.0)
title = 'Title'
ax.text(len(label_strings) * 3, len(y_axis_label) + 2.5, title, horizontalalignment='center', fontsize=60, fontweight='bold', fontname='Arial')
colors = ['green','orange','purple','gray','blue','silver']
x = 0
for i in range(0, len(my_types), 1):
ax.text((x + (len(label_strings) / 2) - 0.75), len(y_axis_label) + 1.5, my_types[i],fontsize=55, fontweight='bold', fontname='Arial')
ax.add_patch(plt.Rectangle((x + .0415, len(y_axis_label) + 0.75), len(label_strings) - (2 * .0415), .5,facecolor=colors[i], clip_on=False))
ax.add_patch(plt.Rectangle((x, 0), len(label_strings), len(y_axis_label), facecolor=colors[i], zorder=0,alpha=0.25, edgecolor='grey'))
x += len(label_strings)
# CODE GOES HERE TO CENTER X-AXIS LABELS...
ax.set_xlim([0, len(my_types) * len(label_strings)])
ax.set_xticklabels([])
ax.tick_params(axis='x', which='both', length=0, labelsize=35)
ax.set_xticks(np.arange(0, len(my_types) * len(label_strings), 1))
ax.set_xticks(np.arange(0, len(my_types) * len(label_strings), 1) + 0.5, minor=True)
ax.set_xticklabels(xticklabels_list, minor=True)
ax.xaxis.set_label_position('top')
ax.xaxis.set_ticks_position('top')
ax.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False) # labels along the bottom edge are off
# CODE GOES HERE TO CENTER Y-AXIS LABELS...
ax.set_ylim([0, len(y_axis_label)])
ax.set_yticklabels([])
ax.tick_params(axis='y', which='both', length=0, labelsize=40)
ax.set_yticks(np.arange(0, len(y_axis_label), 1))
ax.set_yticks(np.arange(0, len(y_axis_label), 1) + 0.5, minor=True)
ax.set_yticklabels(y_axis_label, minor=True) # fontsize
ax.tick_params(
axis='y', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
left=False) # labels along the bottom edge are off
ax.grid(which='major', color='black', zorder=3)
def plot_bars(N,
x_axis_tick_labels,
y_axis_label,
bar1_values,
bar2_values,
color1,
color2,
width,
axis_given):
ind = np.arange(N)
ax=axis_given
ax.bar(ind, bar1_values, width=width, edgecolor='black', color=color1)
ax.bar(ind + width, bar2_values, width=width, edgecolor='black', color=color2)
ax.tick_params(axis='x', labelsize=35)
ax.tick_params(axis='y', labelsize=35)
ax.set_xticklabels(x_axis_tick_labels, fontsize=35)
ax.set_ylabel(y_axis_label, fontsize=35, fontweight='normal')
ax.set_xticks(ind + width / 2)
ax.set_facecolor('white')
ax.spines["bottom"].set_color('black')
ax.spines["left"].set_color('black')
ax.spines["top"].set_color('black')
ax.spines["right"].set_color('black')
def plot_bar_plot_in_given_axis(axis):
my_types = ['a', 'b', 'c', 'd', 'e', 'f']
width = 0.20
color1='red'
color2='blue'
bar1=[100, 110, 120, 130, 140, 150]
bar2=[100, 110, 120, 130, 140, 150]
y_axis_label = 'y axis label'
plot_bars(len(my_types),
my_types,
y_axis_label,
bar1,
bar2,
color1,
color2,
width,
axis)
def plots_all_together():
label_strings=['a','b','c','d','e','f']
xticklabels_list = label_strings * 6
fig = plt.figure(figsize=(5 + 1.5 * len(xticklabels_list), 30 + 1.5))
plt.rc('axes', edgecolor='lightgray')
width = 7
height = 3
width_ratios = [1] * width
height_ratios = [1] * height
gs = gridspec.GridSpec(height, width, height_ratios = height_ratios, width_ratios = width_ratios)
fig.subplots_adjust(hspace=0, wspace=0)
cirle_plot_axis = plt.subplot(gs[0:2, :])
left_bar_plot_axis = plt.subplot(gs[2:, 0:3])
right_bar_plot_axis = plt.subplot(gs[2:, 4:])
plot_bar_plot_in_given_axis(left_bar_plot_axis)
plot_bar_plot_in_given_axis(right_bar_plot_axis)
# plot_bar_plot_in_given_axis(cirle_plot_axis) # No space between plots: good
plot_circles_in_given_axis(cirle_plot_axis,label_strings,label_strings,xticklabels_list,['abc']) # Unnecessary space between plots: how to solve it?
filename = 'test_with_bar_plot_circles.png'
figFile = os.path.join('/Users/burcakotlu/Desktop', filename)
fig.savefig(figFile, dpi=100, bbox_inches="tight")
plt.cla()
plt.close(fig)
plots_all_together()
使用plot_circles_in_given_axis(cirle_plot_axis,label_strings,label_strings,xticklabels_list,['abc']) # 图之间不必要的空间
使用plot_bar_plot_in_given_axis(cirle_plot_axis) # 图之间没有空格