我尝试设置Matplotlib绘图的轴线线条风格,但由于某些原因,它无法工作。我可以将它们设为不可见或变细,但无法更改线条风格。
我的目标是将一个绘图分成两个部分,以展示顶部的异常值。我想将相应的底部/顶部轴线设置为点状,以清楚地显示有一个断点。
我希望以上代码能够绘制顶部图的底部脊柱和底部图的顶部脊柱虚线。
我的目标是将一个绘图分成两个部分,以展示顶部的异常值。我想将相应的底部/顶部轴线设置为点状,以清楚地显示有一个断点。
import numpy as np
import matplotlib.pyplot as plt
# Break ratio of the bottom/top plots respectively
ybreaks = [.25, .9]
figure, (ax1, ax2) = plt.subplots(
nrows=2, ncols=1,
sharex=True, figsize=(22, 10),
gridspec_kw = {'height_ratios':[1 - ybreaks[1], ybreaks[0]]}
)
d = np.random.random(100)
ax1.plot(d)
ax2.plot(d)
# Set the y axis limits
ori_ylim = ax1.get_ylim()
ax1.set_ylim(ori_ylim[1] * ybreaks[1], ori_ylim[1])
ax2.set_ylim(ori_ylim[0], ori_ylim[1] * ybreaks[0])
# Spine formatting
# ax1.spines['bottom'].set_visible(False) # This works
ax1.spines['bottom'].set_linewidth(.25) # This works
ax1.spines['bottom'].set_linestyle('dashed') # This does not work
ax2.spines['top'].set_linestyle('-') # Does not work
ax2.spines['top'].set_linewidth(.25) # Works
plt.subplots_adjust(hspace=0.05)
我希望以上代码能够绘制顶部图的底部脊柱和底部图的顶部脊柱虚线。
我错过了什么?
line.get_linestyle()
是行不通的,因为它只返回'--'
。) - Erlend Magnus Viggenspine.set_linestyle(line.get_linestyle()); spine.set_linewidth(line.get_linewidth()); spine.set_capstyle(line.get_dash_capstyle())
这样的东西。 - ImportanceOfBeingErnest