Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

python - "Clean" way to use words as markers in matplotlib? And make font size and color differ?

Suppose I have the 3x3 matrix below:

[apples 19 3.5]

[oranges 07 2.2]

[grapes 23 7.8]

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

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

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

For example

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

Example

This method has several issues

  • Using extrm{} 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)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...