使用寄生虫轴时去除宿主的脊柱

时间:2018-03-28 11:16:25

标签: python matplotlib multiple-axes

我对python和stackoverflow都很陌生,所以我希望我能发布这个。

我试图用散点图叠加制作条形图。我按照matplotlib网站上的demo_parasite_axes2示例创建了一个主机和一个寄生虫轴。这似乎工作正常。

现在我想删除主轴的顶部和右侧脊柱,但似乎无法做到。我试过host.spines['right'].set_visible(False) 但这没有任何作用。我还试图通过host.spines["top"].set_color('w')将脊椎颜色更改为白色,但这也没有做任何事情。 情节的脚本如下:

使用的数据是大小(5,6)的numpy.ndarray:

print(valval)

[[  1.27225695   1.28820064   5.54155106   4.44582149   6.71393981  
    5.7292093 ]
 [ 70.75471698  79.24528302  65.30612245  81.13207547  74.52830189
    55.05617978]
 [ 22.64150943  16.98113208  27.55102041  15.09433962  19.81132075
    25.84269663]
 [  5.66037736   2.83018868   7.14285714   1.88679245   3.77358491
    11.23595506]
 [  0.           0.           0.           1.88679245   0.94339623
    3.37078652]]

print(valval.shape)
(5, 6)

绘图脚本;

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA

N = valval.shape[1]
SD_1 = valval[1,:]
SD_2 = valval[2,:]
SD_3 = valval[3,:]
SD_4 = valval[4,:]
SD_5 = 100 - SD_1 - SD_2 - SD_3 - SD_4

fig = plt.figure(figsize=(10,6), facecolor=[1,1,1],frameon=False)
host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)


y_axis = host.twinx()
offset = 20
new_fixed_axis = y_axis.get_grid_helper().new_fixed_axis
y_axis.axis["right"] = new_fixed_axis(loc="right",
                                axes=y_axis,
                                offset=(offset, 0))

# Set host y axis
host.set_ylabel("Percentage")
host.axis["left"].label.set_fontsize(20)

# Set parasite y axis
y_axis.set_ylabel("nRMSE")
y_axis.axis["right"].label.set_fontsize(20)
y_axis.set_ylim(1, 10)
# Set parasite axis color
y_axis.axis["right"].label.set_color('r')
y_axis.axis["right"].line.set_color('r')
y_axis.axis["right"].major_ticks.set_color('r')
y_axis.axis["right"].major_ticklabels.set_color('r')

# Bar chart
ind = np.arange(N)
width = 0.9
p1 = host.bar(ind, SD_1, width)
p2 = host.bar(ind, SD_2, width,bottom=SD_1)
p3 = host.bar(ind, SD_3, width,bottom=SD_1+SD_2)
p4 = host.bar(ind, SD_4, width,bottom=SD_1+SD_2+SD_3)
p5 = host.bar(ind, SD_5, width,bottom=SD_1+SD_2+SD_3+SD_4)

# Horizontal lines
host.axhline(68,color='k')
host.axhline(95,color='k')
host.axhline(99,color='k')

# Scatter plot
pp1, = y_axis.plot(ind, valval[0,:],'or')

# Set x axis
host.set_xlim(-0.55, 5.55)
plt.xticks(ind, ('M\_sat','M\_precip','M\_motmax','M\_shice','M\_o2',
        'M\_co2'),rotation='vertical',fontsize=25)

plt.show()

输出: My bar chart with scatter overlay

我希望我的情节看起来像什么(我不想删除所有刺,只有顶部和右边的刺): Bar chat with no spines

1 个答案:

答案 0 :(得分:0)

因此,按照ImportanceOfBeingErnest的建议,我使用了普通的子图而不是axes_grid1.host_subplothost.spines['right'].set_visible(False)工作正常。

我会在这里发布新脚本以防其他人发现它有用:

fig, host = plt.subplots(figsize=(10,6))
fig.subplots_adjust(right=0.75)

par = host.twinx()
par.spines["right"].set_position(("axes",1.05))
par.spines["right"].set_visible(True)

# Needs to make both host.spines and par.spines invisible for the top spine
host.spines["right"].set_visible(False)
host.spines["top"].set_visible(False)
par.spines["top"].set_visible(False)

# Set host y axis
host.set_ylabel("Percentage",size=20)

# parasite y axis
par.set_ylabel("nRMSE",size=20)
par.set_ylim(1, 10)
par.yaxis.label.set_color('r')
par.tick_params(axis='y',colors='r')

# Bar chart
ind = np.arange(N)
width = 0.9
p1 = host.bar(ind, SD_1, width)
p2 = host.bar(ind, SD_2, width,bottom=SD_1)
p3 = host.bar(ind, SD_3, width,bottom=SD_1+SD_2)
p4 = host.bar(ind, SD_4, width,bottom=SD_1+SD_2+SD_3)
p5 = host.bar(ind, SD_5, width,bottom=SD_1+SD_2+SD_3+SD_4)

# Horizontal lines
host.axhline(68,color='k')
host.axhline(95,color='k')
host.axhline(99,color='k')

host.set_xlim(-0.55, 5.55)
plt.show()

这是结果: No top and right spines