gpt4 book ai didi

python - 移动脊柱后有效缓存和恢复 matplotlib 轴参数

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:15 24 4
gpt4 key购买 nike

我的问题

在偏移脊柱后,我在维护应用于 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()

其产量:

one good, one bad

如您所见,偏移书脊后,我丢失了 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)

虽然这确实有效,但它是:

  1. 非常重复
  2. 需要大约两倍的长度才能涵盖出现小刻度标签的可能性。

主要问题:

是否有更有效的方法来实现此目的,而无需手动拾取和恢复 2 个属性 x 2 个轴 x 主要+次要刻度 + 2 个标签?

最佳答案

我修改了你的代码,现在它可以产生相同的蜱虫了。

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

这是输出:

enter image description here

关于python - 移动脊柱后有效缓存和恢复 matplotlib 轴参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21541295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com