Matplotlib颜色条移动第二个x轴

时间:2016-01-24 18:35:15

标签: python matplotlib plot

我正在尝试使用twiny在绘图顶部添加第二个x轴。

如果我制作一个没有颜色条的简单散点图,则顶部x轴与底部x轴正确对齐(MWE在下面):

enter image description here

如果我添加了一个颜色条,则顶部x轴会移位:

enter image description here

我该如何解决这个问题?

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

2 个答案:

答案 0 :(得分:5)

您可以从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()

我认为会解除你的用例。

限制有点不同,因为我坐在当前的主分支上。

enter image description here

如果您需要使用tight_layout这样的东西(需要对填充等进行一些调整):

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

enter image description here

答案 1 :(得分:2)

这感觉就像是一种黑客攻击。但是通过强迫数字绘制。然后获得ax1位置。你可以将ax2设置为相同并重绘图形。

....
cbar.ax.tick_params(labelsize=10)

fig.canvas.draw()
ax2.set_position(ax1.get_position())

plt.show()

enter image description here