Matplotlib颜色条移动第二个X轴 [英] Matplotlib colorbar moves second x axis
本文介绍了Matplotlib颜色条移动第二个X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用
如果我添加一个颜色条,则x轴的顶部会移位:
我该如何解决?
<小时>MWE
将 numpy 导入为 np导入matplotlib.pyplot作为plt从 mpl_toolkits.axes_grid1 导入 make_axes_locatable导入 matplotlib.gridspec 作为 gridspecX = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])Y = np.cos(X * 20)Z = np.sin(X*20)无花果= plt.figure()gs = gridspec.GridSpec(1、2)ax1 = plt.subplot(gs [1])ax2 = ax1.twiny()ax1.set_xlim(-0.2, max(X)+0.2)plt.tick_params(axis ='both',which ='major',labelsize = 10)ax1.minorticks_on()ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)SC = ax1.scatter(X,Y,c = Z)ax1.set_xlabel(原始x轴")ax2.set_xlim(ax1.get_xlim())ax2.set_xticks(X)ax2.set_xticklabels(X2)ax2.set_xlabel("第二个x轴")#彩条.the_divider = make_axes_locatable(ax1)color_axis = the_divider.append_axes("right", size="2%", pad=0.1)cbar = plt.colorbar(SC,cax = color_axis)cbar.set_label('B',fontsize = 10,labelpad = 4,y = 0.5)cbar.ax.tick_params(labelsize=10)plt.show()
解决方案
您可以在
如果你需要使用 tight_layout
这样的东西(这需要对填充等进行一些调整):
将 numpy 导入为 np导入matplotlib.pyplot作为plt导入 matplotlib.gridspec 作为 gridspecX = np.array([0.,0.5,1.,1.5,2.,2.5,3.,3.5,4.])X2 = np.array([122,85,63,50,23,12,7,5,5,2])Y = np.cos(X*20)Z = np.sin(X*20)无花果= plt.figure()gs = gridspec.GridSpec(1、2)right_gs = gridspec.GridSpecFromSubplotSpec(1、2,width_ratios = [30、1],subplot_spec = gs [1],wspace = 0.05)ax1 = fig.add_subplot(right_gs[0])color_axis = fig.add_subplot(right_gs [1])ax2 = ax1.twiny()ax1.set_xlim(-0.2,max(X)+0.2)plt.tick_params(axis='both', which='major', labelsize=10)ax1.minorticks_on()ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)SC = ax1.scatter(X, Y, c=Z, cmap='viridis')ax1.set_xlabel(原始x轴")ax2.set_xlim(ax1.get_xlim())ax2.set_xticks(X)ax2.set_xticklabels(X2)ax2.set_xlabel("第二个x轴")cbar = fig.colorbar(SC,cax = color_axis)cbar.set_label('B',fontsize = 10,labelpad = 4,y = 0.5)cbar.ax.tick_params(labelsize=10)fig.tight_layout()plt.show()
I'm trying to add a second x axis to the top of a plot using twiny.
If I make a simple scatter plot with no colorbar, the top x axis is correctly aligned with the bottom x axis (MWE is below):
If I add a colorbar though, the top x axis is displaced:
How can I fix this?
MWE
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.gridspec as gridspec
X = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])
X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])
Y = np.cos(X*20)
Z = np.sin(X*20)
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
ax1 = plt.subplot(gs[1])
ax2 = ax1.twiny()
ax1.set_xlim(-0.2, max(X)+0.2)
plt.tick_params(axis='both', which='major', labelsize=10)
ax1.minorticks_on()
ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)
SC = ax1.scatter(X, Y, c=Z)
ax1.set_xlabel("Original x-axis")
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(X)
ax2.set_xticklabels(X2)
ax2.set_xlabel("Second x-axis")
# Colorbar.
the_divider = make_axes_locatable(ax1)
color_axis = the_divider.append_axes("right", size="2%", pad=0.1)
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('B', fontsize=10, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=10)
plt.show()
解决方案
You can have the colorbar 'steal' space from more than one ax
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.gridspec as gridspec
X = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])
X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])
Y = np.cos(X*20)
Z = np.sin(X*20)
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
ax1 = plt.subplot(gs[1])
ax2 = ax1.twiny()
ax1.set_xlim(-0.2, max(X)+0.2)
plt.tick_params(axis='both', which='major', labelsize=10)
ax1.minorticks_on()
ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)
SC = ax1.scatter(X, Y, c=Z, cmap='viridis')
ax1.set_xlabel("Original x-axis")
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(X)
ax2.set_xticklabels(X2)
ax2.set_xlabel("Second x-axis")
# Colorbar.
cbar = plt.colorbar(SC, ax=[ax1, ax2])
cbar.set_label('B', fontsize=10, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=10)
plt.show()
which I think will un-block your use case.
Limits are a bit different because I am sitting on the current master branch.
If you need to use tight_layout
something like this (which requires some tuning on padding etc):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
X = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])
X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])
Y = np.cos(X*20)
Z = np.sin(X*20)
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
right_gs = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[30, 1], subplot_spec=gs[1], wspace=0.05)
ax1 = fig.add_subplot(right_gs[0])
color_axis = fig.add_subplot(right_gs[1])
ax2 = ax1.twiny()
ax1.set_xlim(-0.2, max(X)+0.2)
plt.tick_params(axis='both', which='major', labelsize=10)
ax1.minorticks_on()
ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)
SC = ax1.scatter(X, Y, c=Z, cmap='viridis')
ax1.set_xlabel("Original x-axis")
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(X)
ax2.set_xticklabels(X2)
ax2.set_xlabel("Second x-axis")
cbar = fig.colorbar(SC, cax=color_axis)
cbar.set_label('B', fontsize=10, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=10)
fig.tight_layout()
plt.show()
这篇关于Matplotlib颜色条移动第二个X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文