Reputation: 41
I'm new to both python and stackoverflow so I hope I'm posting this right.
I'm trying to make a bar chart with scatter overlay. I followed the demo_parasite_axes2 example on the matplotlib website to create a host and a parasite axis. That seems to work fine.
Now I want to remove the top and right spines of the host axis but can't seem to do it. I tried host.spines['right'].set_visible(False)
but this does nothing. I also tried to change the spine colour to white by host.spines["top"].set_color('w')
but this doesn't do anything either.
The script for the plot is below:
Data used is a numpy.ndarray of size (5, 6):
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)
The plotting script;
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()
What I want my plot to look like (I don't want to remove all spines, just the top and the right ones):
Upvotes: 3
Views: 647
Reputation: 41
So following suggestion by ImportanceOfBeingErnest, I used normal subplots instead of axes_grid1.host_subplot
and host.spines['right'].set_visible(False)
works fine.
I will post the new script here in case anyone else finds it useful:
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()
This is the result: No top and right spines
Upvotes: 1