2

In this latitude/longitude scatter plot with colour coded points:

I want to have no axis but have a shared label for the x axis and the y axis. Everything I try with axis labels fails as the labels don't show without the axis being visible.

Working code without these errors is listed below.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("_test.csv")
power = df['n']
lat = df['latitude']
lon = df['longitude']

df = pd.read_csv("shifted3.csv")
power = df['n']
lat = df['latitude']
lon = df['longitude']
plt.subplot(121)
plt.scatter(lon, lat, c=power,s=65, vmin=0, vmax=3700)  
# c= sets how the points are coloured, s= point size, vmin/max colour lims
plt.title('a) AGW')
plt.ylim(ymin=51.44,ymax=51.73)
plt.xlim(xmin=1.42, xmax=1.63)
plt.axis('off')
cbar = plt.colorbar()
cbar.ax.set_yticklabels(['0','','','','','','','','','3600'])

plt.subplot(122)
#plt.figure(figsize=(5,10))
plt.scatter(lon, lat, c=power,s=65, vmin=0, vmax=3700)  
# c= sets how the points are coloured, s= point size, vmin/max colour lims
plt.title('b) no event')
plt.xlim(xmin=2.23, xmax=2.45)
plt.ylim(ymax=52.09)
plt.axis('off')
# #
cbar = plt.colorbar()
cbar.ax.set_yticklabels(['0','','800','','1600','','2400','','','3600'])
cbar.set_label('Power (kW)', rotation=270, labelpad=+12)
#labelpad + moves legend to right, - to left

plt.show()
squar_o
  • 557
  • 2
  • 11
  • 36
  • does this answer your question? http://stackoverflow.com/questions/29041326/3d-plot-with-matplotlib-hide-axes-but-keep-axis-labels – periphreal Jan 12 '17 at 14:10
  • 1
    It's hard to understand what you really try to achieve. "I want to have no axis" - the plot you show does not have any axes. "...but have a shared label for the x axis and the y axis" How can x and y share a label? Where would that label sit? – ImportanceOfBeingErnest Jan 12 '17 at 16:21
  • Apologies @ImportanceOfBeingErnest, I meant a shared x label under the subplots and a single y axis at the left hand side of the two plots. – squar_o Jan 12 '17 at 17:52

1 Answers1

4

Using plt.axis("off") kills everything: the axes borders, the labels, the tickmarks and ticklabels.

In case one wants to keep some of those, they have to individually be turned off.
Ticks can be made invisible via ax.xaxis.set_visible(False).
The border can be set invisible via ax.spines["bottom"].set_visible(False).

A label below the whole figure can be set via plt.figtext(x, y, text).

Putting this all together, gives

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


power = np.random.rand(32)*3600
lat = 51.45 + 0.26*np.random.rand(32)
lon = 1.44 + 0.18*np.random.rand(32)


plt.subplot(121)
plt.scatter(lon, lat, c=power,s=65, vmin=0, vmax=3700)  
# c= sets how the points are coloured, s= point size, vmin/max colour lims
plt.title('a) AGW')
plt.ylim(ymin=51.44,ymax=51.73)
plt.xlim(xmin=1.42, xmax=1.63)
#plt.axis('off')
cbar = plt.colorbar()
cbar.ax.set_yticklabels(['0','','','','','','','','','3600'])

ax = plt.gca()
ax.set_ylabel("Some y label")

#Make x axis and all spines but left one invisible
ax.xaxis.set_visible(False)
for position in ["right", "top", "bottom"]:
    ax.spines[position].set_visible(False)
# Only show ticks on the left spine
ax.yaxis.set_ticks_position('left')

plt.subplot(122)
#plt.figure(figsize=(5,10))
plt.scatter(lon, lat, c=power,s=65, vmin=0, vmax=3700)  
# c= sets how the points are coloured, s= point size, vmin/max colour lims
plt.title('b) no event')
plt.xlim(xmin=1.42, xmax=1.63)
plt.ylim(ymin=51.44,ymax=51.73)
#plt.axis('off')
# #
cbar = plt.colorbar()
cbar.ax.set_yticklabels(['0','','800','','1600','','2400','','','3600'])
cbar.set_label('Power (kW)', rotation=270, labelpad=+12)
#labelpad + moves legend to right, - to left
ax = plt.gca()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
for position in ["left","right", "top", "bottom"]:
    ax.spines[position].set_visible(False)
# Add some text below the subplots
plt.figtext(0.5, 0.05, "Some x label beneath the whole figure", ha="center")

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712