简体   繁体   English

如何更改各个颜色条标签的字体粗细?

[英]How to change the font weight of individual colorbar labels?

I would like to have different font weights for each of my colorbar labels. 我想为每个colorbar标签设置不同的字体权重。

I have tried to let LaTeX format the labels in the following way: 我试图让LaTeX以下列方式格式化标签:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(50, 50)/20)
cbar = ax.figure.colorbar(im, ticks=np.arange(0, 0.05, 0.01))
cbar.ax.set_yticklabels([r'{\fontsize{50pt}{3em}\selectfont{}{0}}',
                         r'{\fontsize{40pt}{3em}\selectfont{}{0.01}}',
                         r'{\fontsize{30pt}{3em}\selectfont{}{0.03}}',
                         r'{\fontsize{20pt}{3em}\selectfont{}{0.03}}',
                         r'{\fontsize{10pt}{3em}\selectfont{}{0.04}}',
                         r'{\fontsize{1pt}{3em}\selectfont{}{0.05}}', ])

but this only updates the text of the labels to the whole string (eg, {\\fontsize{50pt}{3em}\\selectfont{}{0}}). 但这只会将标签的文本更新为整个字符串(例如,{\\ fontsize {50pt} {3em} \\ selectfont {} {0}})。 The pyplot TeX demo works for me. pyplot TeX 演示适用于我。 Even if this solution would work it would not be ideal as I would probably need to specify everything manually. 即使这个解决方案能够工作也不理想,因为我可能需要手动指定所有内容。

Much more convenient would be something like in this question . 在这个问题中会更加方便。 There, I learned that the font size of single labels of the regular x and y axis can be updated by calling 在那里,我了解到常规x和y轴的单个标签的字体大小可以通过调用来更新

label = axes.yaxis.get_major_ticks()[2].label
label.set_fontsize(size)

replacing set_fontsize by set_fontweight correctly updates the weight of the selected label. 通过set_fontweight替换set_fontsize set_fontweight正确更新所选标签的权重。 Unfortunately I could not find the equivalent of axes.yaxis.get_major_ticks()[2].label for the colorbar. 不幸的是,我找不到相应的axes.yaxis.get_major_ticks()[2].label为colorbar。

Is it possible to change the font weight of individual labels of the colorbar directly? 是否可以直接更改颜色条的各个标签的字体粗细? With directly I mean without using a workaround like plotting some new text above existing labels. 直接我的意思是不使用在现有标签上方绘制一些新文本的解决方法。 If this is not possible a solution plotting text above existing labels which automatically uses the position and content the previous labels and only adjusts the font weight would also be appreciated. 如果这不可能,也可以理解在现有标签上方绘制文本的解决方案,其自动使用先前标签的位置和内容并且仅调整字体粗细。

Thanks! 谢谢!

As pointed out by @ImportanceOfBingErnest , set_fontweight works for setting the weight of single colorbar labels too. 正如@ImportanceOfBingErnest所指出的, set_fontweight适用于设置单个颜色条标签的权重。 I had to try a couple of things to find which call would give me the text objects defining the colorbar labels. 我不得不尝试一些事情来找到哪个调用会给我定义颜色条标签的文本对象。 They are accessible in cbar.ax.get_yticklabels() . 可以在cbar.ax.get_yticklabels()中访问它们。 The code snippet below now properly changes the weight of the second colorbar label: 下面的代码段正确地更改了第二个颜色条标签的重量:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
im = ax.imshow(np.random.rand(50, 50)/20)
cbar = ax.figure.colorbar(im, ticks=np.arange(0, 0.05, 0.01))
cbar.ax.get_yticklabels()[1].set_fontweight(1000)
plt.show()

Output of code (not enough reputation for inline images) 代码输出(内联图像的声誉不足)

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

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