我想在 matplotlib 中使用 GridSpec 在圖中繪制子圖。
在第一行(不同行數)中,我想放置一個帶圓圈的網格。
在倒數第二行,我想放置一個圖例。
在最后一行,我想放置一個顏色條。
但似乎我的顏色條占用的空間比我給的更多。
你有什么想法來解決它嗎?
這是重新生成它的代碼:
from matplotlib import pyplot as plt
import matplotlib.cm as matplotlib_cm
import numpy as np
import matplotlib as mpl
import os
def plot_legend_in_given_axis(ax, fontsize):
diameter_labels = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
row_labels = ['circle']
ax.grid(which="major", color="white", linestyle='-', linewidth=3)
ax.set_aspect(1.0)
for row_index, row_label in enumerate(row_labels):
for diameter_index, diameter_label in enumerate(diameter_labels):
circle=plt.Circle((diameter_index 0.5, row_index 0.5), radius=(diameter_label/(2*1.09)), color='gray', fill=True)
ax.add_artist(circle)
ax.set_xlim([0, len(diameter_labels)])
ax.set_xticklabels([])
ax.tick_params(axis='x', which='minor', length=0, labelsize=12)
ax.set_xticks(np.arange(0, len(diameter_labels), 1))
ax.xaxis.set_ticks_position('bottom')
ax.set_xlabel('Very long\nx axis label', fontsize=fontsize, labelpad=5)
ax.set_ylim([0, len(row_labels)])
ax.set_yticklabels([])
ax.tick_params(axis='y', which='minor', length=0, labelsize=12)
ax.set_yticks(np.arange(0, len(row_labels), 1))
ax.grid(which='major', color='black', linestyle='-', linewidth=1)
def main(width, height, xaxis_labels, yaxis_labels):
width_multiply = 1.5
height_multiply = 1.5
figure_width = int(width_multiply * width)
figure_height = int(height_multiply * height)
fig = plt.figure(figsize=(figure_width, figure_height))
grid = plt.GridSpec(width, height, hspace=0, wspace=0)
ax = fig.add_subplot(grid[0:-2, :])
ax.set_aspect(1.0)
legend_ax = fig.add_subplot(grid[-2, :])
plot_legend_in_given_axis(legend_ax, 20)
color_bar_ax = fig.add_subplot(grid[-1, :])
cmap = matplotlib_cm.get_cmap('YlOrRd')
v_min = 2
v_max = 20
norm = plt.Normalize(v_min, v_max)
bounds = np.arange(v_min, v_max 1, 2)
cb = mpl.colorbar.ColorbarBase(color_bar_ax, cmap=cmap, norm=norm, ticks=bounds, spacing='proportional', orientation='horizontal')
cb.ax.tick_params(labelsize=20)
cb.set_label("-log10 (q-value)", horizontalalignment='center', rotation=0, fontsize=20)
plt.xlim([0, width])
ax.set_xticks(np.arange(0, width 1, 1))
plt.ylim([1, height])
ax.set_yticks(np.arange(0, height 1, 1))
# Plot the circles with color in the grid
for yaxis_index, yaxis_label in enumerate(yaxis_labels):
for xaxis_index, xaxis_label in enumerate(xaxis_labels):
circle = plt.Circle((xaxis_index 0.5, yaxis_index 0.5), 0.4, color=cmap(norm(10)), fill=True)
ax.add_artist(circle)
ax.grid()
for edge, spine in ax.spines.items():
spine.set_visible(True)
spine.set_color('black')
figures_path = os.path.join('/Users', 'burcakotlu', 'Desktop')
figFile = os.path.join(figures_path, 'ColorBar_Using_GridSpec.png')
fig.savefig(figFile, dpi=100, bbox_inches="tight")
plt.cla()
plt.close(fig)
width = 5
height = 5
xaxis_labels = ["x%d" %i for i in range(width)]
yaxis_labels = ["y%d" %i for i in range(height)]
main(width, height, xaxis_labels, yaxis_labels)

uj5u.com熱心網友回復:
這個問題來自這樣一個事實,即您正在使用plt.xlim([0, width])并在定義顏色條的軸后立即 plt.ylim([1, height])設定第一個子圖(帶軸ax)的軸color_bar_ax范圍。相反,如果你使用ax.set_xlim([0, width]),并ax.set_ylim([1, height])那么你指定你現在指的是你的拳頭副區(ax),一切運行良好。
我從進行這些更改中獲得的輸出如下所示:

可選修改:
最重要的是,您可以更改分布圖的方式GridSpec,以便圖和標簽之間沒有重疊。下面,我將網格上第一個子圖的位置從grid[0:-2, :])更改為grid[0:-3, :],第二個子圖從grid[-2,:]到grid[-3:-1, :]。然后輸出變成:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371891.html
標籤:matplotlib 颜色条
上一篇:如何用計數和百分比注釋水平條形圖
