简体   繁体   English

matplotlib图例标签在键下?

[英]matplotlib legend label under key?

How to adjust label location relate to key?I reclassified the data and displayed a discrete corbar which looks like multi-handles legend. 如何调整与标签相关的标签位置?我对数据进行了重新分类,并显示了一个类似于多句柄图例的离散corbar。 Actually ,I couldn't find any parameters about the location of labels(text or numbers).The default setting is keys in left while label in right. 实际上,我找不到有关标签位置的任何参数(文本或数字)。默认设置是在左侧输入键,而在右侧输入标签。 Could I change the position? 我可以改变位置吗? such as labels under keys or above. 例如按键下方或上方的标签。 My purpose is to show the legend as follows (label under key and no space between keys: 我的目的是显示图例,如下所示(键下的标签,键之间没有空格: 在此处输入图片说明

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

It seems that there is no parameters to adjust location of labels. 似乎没有参数可以调整标签的位置。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM