问题描述
我需要注释几行数据-长度不同的pylab矩形.在搜索matplotlib文档和谷歌搜索之后,我找不到一种为注释的不同部分提供不同大小的方法.
I need to annotate a pylab rectangle with few data lines - which are of different length.Searching through the matplotlib documentation, and Googling, I could not find a way to give different parts of an annotation different sizes.
以下代码段演示了该问题:
The following snippet demonstrates the problem:
import pylab
from matplotlib.patches import Rectangle
pylab.rcParams['verbose.level'] = 'debug-annoying'
def draw_rectangle(lower, upper, entry):
ax = pylab.subplot(111)
r = Rectangle( lower, upper[0]-lower[0], upper[1] - lower[1],
edgecolor='k')
ax.add_patch(r)
textRank = str(entry['rank'])
textTeamName = entry['teamName']
textSubmissionDate = entry['submissionDate']
text = textRank + "\n" + textTeamName + "\n" + textSubmissionDate
ax.add_artist(r)
rx, ry = r.get_xy()
cx = rx + r.get_width()/2.0
cy = ry + r.get_height()/2.0
ax.annotate(text, (cx, cy), color='w', weight='bold', ha='center', va='center', size=14)
if __name__ == '__main__':
entry = {'rank': 22, 'submissionDate': '12/21/2012 4:58:45 AM', 'teamName': 'A very very very very very very very very very very long name'}
lower = [0,0]
upper = [1,1]
draw_rectangle(lower, upper, entry)
pylab.show()
例如,是否有一种方法可以在'teamName'的字体大小与'rank'的字体大小不同的地方添加注释?
另一个问题是我找不到字体大小与缩放相关的方法:
我正在创建一个树形图,即pylab窗口中填充了不同大小的矩形.如果要为不同的矩形创建批注,则长数据的大小必须非常小(以保持在各个矩形的边界内).但是,我希望在放大时,长数据行的字体大小增大.
E.g., is there a way to have the annotation where the font size of 'teamName' is different than the font size of 'rank'?
Another problem is I could not find a way for the font size to correlate to the zooming:
I'm creating a treemap plot, namely, the pylab window is filled with different sized rectangles. If I want to create annotations for the different rectangles, the long data need to be of very small size (to stay within the respective rectangles' boundaries). However, I'd like the font size of the long data line to grow as I zoom in.
推荐答案
我找不到创建具有不同字体的注释的方法,因此我将创建一个辅助函数,该函数将计算在每种字体上使用的字体粗细注释的一行,带有相关的(cx,cy),
,然后调用ax.annotate多次.
I couldn't find a way to create an annotation with different fonts, so I'll create a helper function that will calculate the font weight to use on each line of the annotation, with the relevant (cx, cy),
and then call ax.annotate several times.
这篇关于matplotlib的相同批注中的字体大小是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!