【问题标题】:Matplotlib pyplot 2d scatter no axes, add shared axis labelsMatplotlib pyplot 2d scatter no axes,添加共享轴标签
【发布时间】:2017-05-27 13:07:58
【问题描述】:

在这个带有颜色编码点的纬度/经度散点图中:

我想没有轴,但有一个 x 轴和 y 轴的共享标签。我尝试使用轴标签的所有操作都失败了,因为在轴不可见的情况下标签不会显示。

下面列出了没有这些错误的工作代码。

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()

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/29041326/…
  • 很难理解你真正想要达到的目标。 “我想没有轴”-您显示的图没有任何轴。 “...但是 x 轴和 y 轴有一个共享标签” x 和 y 如何共享一个标签?那个标签会放在哪里?
  • 道歉@ImportanceOfBeingErnest,我的意思是子图下方的共享 x 标签和两个图左侧的单个 y 轴。

标签: python python-2.7 matplotlib axis-labels axes


【解决方案1】:

使用plt.axis("off") 会杀死一切:轴边界、标签、刻度线和刻度线。

如果想保留其中一些,则必须单独关闭它们。
可以通过 ax.xaxis.set_visible(False) 使刻度不可见。
可以通过ax.spines["bottom"].set_visible(False)将边框设置为不可见。

整个图形下方的标签可以通过plt.figtext(x, y, text)设置。

把这一切放在一起,给出了

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()

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多