English 中文(简体)
文本部分颜色化
原标题:Partial coloring of text
Is there a way in matplotlib to partially specify the color of a string? Example: plt.ylabel("Today is cloudy.") How can I show "today" as red, "is" as green and "cloudy." as blue?
问题回答
I only know how to do this non-interactively, and even then only with the PS backend. To do this, I would use Latex to format the text. Then I would include the color package, and set your colors as you wish. Here is an example of doing this: import matplotlib matplotlib.use( ps ) from matplotlib import rc rc( text ,usetex=True) rc( text.latex , preamble= usepackage{color} ) import matplotlib.pyplot as plt plt.figure() plt.ylabel(r extcolor{red}{Today} + r extcolor{green}{is} + r extcolor{blue}{cloudy.} ) plt.savefig( test.ps ) This results in (converted from ps to png using ImageMagick, so I could post it here):
Here s the interactive version. Edit: Fixed bug producing extra spaces in Matplotlib 3. import matplotlib.pyplot as plt from matplotlib import transforms def rainbow_text(x,y,ls,lc,**kw): """ Take a list of strings ``ls`` and colors ``lc`` and place them next to each other, with text ls[i] being shown in color lc[i]. This example shows how to do both vertical and horizontal text, and will pass all keyword arguments to plt.text, so you can set the font size, family, etc. """ t = plt.gca().transData fig = plt.gcf() plt.show() #horizontal version for s,c in zip(ls,lc): text = plt.text(x,y,s+" ",color=c, transform=t, **kw) text.draw(fig.canvas.get_renderer()) ex = text.get_window_extent() t = transforms.offset_copy(text._transform, x=ex.width, units= dots ) #vertical version for s,c in zip(ls,lc): text = plt.text(x,y,s+" ",color=c, transform=t, rotation=90,va= bottom ,ha= center ,**kw) text.draw(fig.canvas.get_renderer()) ex = text.get_window_extent() t = transforms.offset_copy(text._transform, y=ex.height, units= dots ) plt.figure() rainbow_text(0.05,0.05,"all unicorns poop rainbows ! ! !".split(), [ red , orange , brown , green , blue , purple , black ], size=20)
Extending Yann s answer, LaTeX coloring now also works with PDF export: import matplotlib from matplotlib.backends.backend_pgf import FigureCanvasPgf matplotlib.backend_bases.register_backend( pdf , FigureCanvasPgf) import matplotlib.pyplot as plt pgf_with_latex = { "text.usetex": True, # use LaTeX to write all text "pgf.rcfonts": False, # Ignore Matplotlibrc "pgf.preamble": [ r usepackage{color} # xcolor for colours ] } matplotlib.rcParams.update(pgf_with_latex) plt.figure() plt.ylabel(r extcolor{red}{Today} + r extcolor{green}{is} + r extcolor{blue}{cloudy.} ) plt.savefig("test.pdf") Note that this python script sometimes fails with Undefined control sequence errors in the first attempt. Running it again is then successful.
After trying all the methods above, I return back to my stupid but easy method, using plt.text. The only problem is that you need to adjust the spaces between each word. You may need to adjust the positions several times, but I still like this way, because it saves you from installing tex compilers, does not require any special backends, and free you from configuring matplotlib rc and configure back, or it may slows down your other plots, due to usetex=True import matplotlib.pyplot as plt fig, ax = plt.subplots() label_x = -0.15 ax.text(label_x, 0.35, r"Today", color= red , rotation= vertical , transform=ax.transAxes) ax.text(label_x, 0.5, r"is", color= green , rotation= vertical , transform=ax.transAxes) ax.text(label_x, 0.55, r"cloudy", color= blue , rotation= vertical , transform=ax.transAxes)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签