matplotlib图例标签在键下?

时间:2019-03-24 08:07:57

标签: python matplotlib

如何调整与键相关的标签位置?我对数据进行了重新分类,并显示了一个离散的corbar,它看起来像多句柄图例。实际上,我找不到有关标签位置的任何参数(文本或数字)。默认设置是在左侧输入键,而在右侧输入标签。我可以改变位置吗?例如按键下方或上方的标签。我的目的是显示图例,如下所示(键下的标签,键之间没有空格: enter image description here

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np

data  = np.random.randint(8, size=(100,100))
cmap = plt.cm.get_cmap('PiYG', 8) 
plt.pcolormesh(data,cmap = cmap,alpha = 0.75)
# Set borders in the interval [0, 1]
bound = np.linspace(0, 1, 9)
# Preparing borders for the legend
bound_prep = np.round(bound * 7, 2)
# Creating 8 Patch instances
plt.legend([mpatches.Patch(color=cmap(b)) for b in bound[:-1]],
       ['{}'.format(bound_prep[i]) for i in range(8)],
       bbox_to_anchor=(0,-0.25,1,0.2),ncol=len(bound))

似乎没有参数可以调整标签的位置。

1 个答案:

答案 0 :(得分:0)

import matplotlib.pyplot as plt
import numpy as np

data  = np.random.randint(8, size=(100,100))
cmap = plt.cm.get_cmap('PiYG', 8)
fig, ax = plt.subplots()
pcm = ax.pcolormesh(data,cmap = cmap,alpha = 0.75, vmin=0, vmax=8)
fig.colorbar(pcm, ax=ax)
plt.show()

enter image description here