簡體   English   中英

Matplotlib pyplot 2d 不散布軸,添加共享軸標簽

[英]Matplotlib pyplot 2d scatter no axes, add shared axis labels

在這個帶有顏色編碼點的緯度/經度散點圖中:

我想沒有軸,但有一個 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()

使用plt.axis("off")殺死一切:軸邊界、標簽、刻度線和刻度標簽。

如果想要保留其中一些,則必須單獨關閉它們。
可以通過ax.xaxis.set_visible(False)使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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM