I have been using cartopy with matplotlib in python 2.7 to visualize some data on maps. But, when I try to remove the axes spines (top and right ones) with the following code snippet:
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
It wouldn't work. Setting the spine thickness doesn't work as well:
ax1.spines['left'].set_linewidth(3)
ax1.spines['bottom'].set_linewidth(3)
The above lines of code do not have an effect on the plots when I am using a cartopy projection. But if I use the regular matplotlib axes, they work fine.
I have tried the rcParams method mentioned here as well with no avail.
I am including the stripped down version of my code below:
lons=np.arange(10, 50, .5)
lats=np.arange(7, 44, .5)
xticks=np.arange(math.ceil(lons.min()),
math.floor(lons.max())+4, 4)
yticks=np.arange(math.ceil(lats.min()),
math.floor(lats.max())+4, 4)
fig=plt.figure()
ax1=fig.add_subplot(111,
projection=ccrs.PlateCarree())
ax1.set_extent([lons[0], lons[-1],
lats[0], lats[-1]],
crs=ccrs.PlateCarree())
ax1.coastlines()
ax1.set_xticks(xticks)
ax1.set_yticks(yticks)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_linewidth(3)
ax1.spines['bottom'].set_linewidth(3)
plt.show()
I am using miniconda to manage the packages and the version of the relevant modules used are as follows:
Python - 2.7.16
matplotlib - 2.3.3
cartopy - 0.17.0
So, how can I remove those spines when using cartopy projections?
UPDATE
I dig deeper and found these:
frameon=False does not remove map frame
Cannot change the border width of the cartopy axes
Which are related to my problem. From the information on those posts I have come up with a workaround for my problem:
First, you have to turn off the axes, or the spines, with the following code.
ax1.outline_patch.set_visible(False)
Then you can use axis.spines[''] method to turn on individual spines like the following.
ax1.spines['left'].set_visible(True)
ax1.spines['bottom'].set_visible(True)
And then set the spine line widths:
ax1.spines['left'].set_linewidth(3)
ax1.spines['bottom'].set_linewidth(3)
Here's the result:

Is there a more proper way to do what I want?