“清洁"matplotlib中使用单词作为标记的方法?并使字体大小和颜色不同? [英] "Clean" way to use words as markers in matplotlib? And make font size and color differ?

查看:31
本文介绍了“清洁"matplotlib中使用单词作为标记的方法?并使字体大小和颜色不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我下面有3x3矩阵:

Suppose I have the 3x3 matrix below:

[苹果 19 3.5]

[apples 19 3.5]

[橙子 07 2.2]

[oranges 07 2.2]

[葡萄 23 7.8]

[grapes 23 7.8]

只有在现实生活中,矩阵有几十行,而不仅仅是三行.

Only in real life the matrix has dozens of rows, not just three.

我想创建一个XY图,其中第二列是X坐标,第三列是Y坐标,单词本身(即第一列)是标记(因此没有点,线或任何其他标记)其他符号).

I want to create an XY plot where the second column is the X coordinate, the third column is the Y coordinate, and the words themselves (i.e., the first column) are the markers (so no dots, lines, or any other symbols).

我还希望每个单词的字体大小由第二列确定(例如,在上面的示例中,这意味着使葡萄"的大小大约是橙色"的三倍).

I also want the font size of each word to be determined by the second column (in the example above, that means making "grapes" have about three times the size of "oranges", for instance).

最后,我想用与第三列相对应的红色至蓝色比例为单词上色,其中0 =最暗的红色和10 =最暗的蓝色.

Finally, I want to color the words on a red-to-blue scale corresponding to the third column, with 0 = darkest red and 10 = darkest blue.

在 Python 2.x 中最好的方法是什么?我知道我可以使用 matplotlib 的注释"和文本"来做很多(如果不是全部)这些事情,但不知何故,这感觉像是一种解决方法.当然,必须有一种方法将这些单词声明为标记(因此我不必将它们视为注释")?也许是 matplotlib 之外的东西?有没有人做过类似的事情?

What's the best way to go about it in Python 2.x? I know I can use matplotlib's "annotate" and "text" to do many (if not all) of those things, but somehow that feels like a workaround. Surely there must be a way of declaring the words to be markers (so I don't have to treat them as "annotations")? Perhaps something outside matplotlib? Has anyone out there ever done something similar?

推荐答案

因为你不想使用 annotatetext 下一个最好的事情是 py.scatter 将接受标记

As you did not want to use annotate or text the next best thing is py.scatter which will accept a marker

``'$...$'``                    render the string using mathtext.

例如

import pylab as py

data = [["peach", 1.0, 1.0], 
        ["apples", 19, 3.5], 
        ["oranges", 7, 2.2], 
        ["grapes", 23, 7.8]]

for item in data:
    py.scatter(item[1], item[2], s=700*item[1], 
           c=(item[2]/10.0, 0, 1 - item[2]/10.0), 
           marker=r"$ {} $".format(item[0]), edgecolors='none' )

py.show()

此方法有几个问题

  • 在数学文本中使用 \ textrm {} 使其不斜体似乎会破坏matplotlib
  • 字母大小需要手动调整(因此调整为700)
  • Using \textrm{} in the math text so that it is not italic appears to break matplotlib
  • The letters sizes need to be adjusted by hand (hence the factor of 700)

最好使用颜色图而不是简单地定义 RGB 颜色值.

It would probably be better to use a colormap rather than simply defining the RGB color value.

这篇关于“清洁"matplotlib中使用单词作为标记的方法?并使字体大小和颜色不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆