在抵消棘刺后,我无法保持应用于matplotlib Axes
对象的格式和修改。
考虑以下简化的工作流程:
%matplotlib inline
import matplotlib.pyplot as plt
def funky_formatting(ax):
ax.set_xticks([0.1, 0.2, 0.5, 0.7, 0.9])
ax.set_xticklabels(list('abcde'), rotation=60)
ax.set_xticks([0.4, 0.6, 0.8], minor=True)
ax.set_xticklabels(list('xzy'), rotation=-60, minor=True)
ax.set_yticks([0.2, 0.5, 0.7, 0.8])
ax.set_yticklabels(list('ABCD'), rotation=35)
ax.tick_params(axis='both', labelsize=18, labelcolor='r')
ax.set_ylabel('r$y_{\mathrm{ii}}$ test', color='b', fontweight='extra bold', fontsize=20)
ax.set_xlabel('r$y_{\mathrm{ii}}$ test', color='r', fontweight='light', fontsize=16)
def offset_spines(ax):
for spine in ax.spines.values():
spine.set_position(('outward', 10))
# create two axes
fig, axes = plt.subplots(nrows=2)
# format both axes the same way:
for ax in axes:
funky_formatting(ax)
# offset the spines of only the top subplot
offset_spines(axes[0])
fig.tight_layout()
哪个收益率:
正如你所看到的,在抵消刺之后,我丢失了我的x / y标签,勾选位置和勾选标签,以及(某些)勾选标签格式。不幸的是,我无法在其余的轴格式化之前偏移棘刺,因为我的目标是创建一个通用函数来处理由其他函数创建的轴,这些函数的轴都格式不同。
可以手动缓存很多这些属性:
# cache the properties - x-axis
xlabels = [t.get_text() for t in ax.get_xticklabels()]
xlabelrot = ax.get_xticklabels()[0].get_rotation()
xticks = ax.get_xticks()
xlabel = ax.get_xlabel()
# cache the properties - y-axis
ylabels = [t.get_text() for t in ax.get_yticklabels()]
ylabelrot = ax.get_yticklabels()[0].get_rotation()
yticks = ax.get_yticks()
ylabel = ax.get_ylabel()
# offset spines
for spine in ax.spines.values():
spine.set_position(('outward', offset))
# restore properties - x-axis
ax.set_xticks(xticks)
ax.set_xticklabels(xlabels, rotation=xlabelrot)
ax.set_xlabel(xlabel)
# restore properties - y-axis
ax.set_yticks(yticks)
ax.set_yticklabels(ylabels, rotation=ylabelrot)
ax.set_ylabel(ylabel)
虽然这样做,但它是:
有没有更有效的方法来实现这一点,而无需手动拾取和恢复2个属性x 2轴x主要+次要刻度+ 2个标签?
答案 0 :(得分:1)
我修改了你的代码,它现在可以生成相同的刻度。
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.artist import ArtistInspector
def funky_formatting(ax):
ax.set_xticks([0.1, 0.2, 0.5, 0.7, 0.9])
ax.set_xticklabels(list('abcde'), rotation=60)
ax.set_yticks([0.2, 0.5, 0.7, 0.8])
ax.set_yticklabels(list('ABCD'), rotation=35)
ax.tick_params(axis='both', labelsize=18, labelcolor='r')
ax.set_ylabel('r$y_{\mathrm{ii}}$ test', color='b', fontweight='extra bold', fontsize=20)
ax.set_xlabel('r$y_{\mathrm{ii}}$ test', color='r', fontweight='light', fontsize=16)
def try_update(artist, p):
for k,v in p.iteritems():
try:
artist.update({k:v})
except:
pass
def offset_spines(ax):
for spine in ax.spines.values():
paxis = spine.axis.properties()
ptick = [label.properties() for label in spine.axis.get_ticklabels()]
spine.set_position(('outward', 10))
try_update(spine.axis, paxis)
for label, p in zip(spine.axis.get_ticklabels(), ptick):
p.pop("transform")
try_update(label, p)
# create two axes
fig, axes = plt.subplots(nrows=2)
# format both axes the same way:
for ax in axes:
funky_formatting(ax)
# offset the spines of only the top subplot
offset_spines(axes[0])
fig.tight_layout()
这是输出: