You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Doc]: Rewrite rainbow_text example to use annotate() #25941
As noted in #25907 (comment), the rainbow_text example can likely be rewritten in a simpler fashion using annotate(), similarly to #25905, more simply than with explicit offset_copies.
I went through the Contributing Guidelines, it is mentioned that we can create PR and work on it. But, I have a doubt, which branch should I have to use for PR? Please help!
importmatplotlib.pyplotaspltdefrainbow_text(x, y, strings, colors, orientation='horizontal',
ax=None, **kwargs):
""" Take a list of *strings* and *colors* and place them next to each other, with text strings[i] being shown in colors[i]. Parameters ---------- x, y : float Text position in data coordinates. strings : list of str The strings to draw. colors : list of color The colors to use. orientation : {'horizontal', 'vertical'} ax : Axes, optional The Axes to draw into. If None, the current axes will be used. **kwargs All other keyword arguments are passed to plt.annotate(), so you can set the font size, family, etc. """ifaxisNone:
ax=plt.gca()
assertorientationin ['horizontal', 'vertical']
iforientation=='vertical':
kwargs.update(rotation=90, verticalalignment='bottom')
fors, cinzip(strings, colors):
ax.annotate(s+" ", (x, y), color=c, **kwargs)
iforientation=='horizontal':
x+=0.1else:
y+=0.1words="all unicorns poop rainbows ! ! !".split()
colors= ['red', 'orange', 'gold', 'lawngreen', 'lightseagreen', 'royalblue',
'blueviolet']
plt.figure(figsize=(6, 6), dpi=100)
rainbow_text(0.1, 0.05, words, colors, size=18)
rainbow_text(0.05, 0.1, words, colors, orientation='vertical', size=18)
plt.show()
@anntzer The code above is my attempt to rewrite rainbow_text simpler fashion using annotate(), however, I am having an issue formatting text appropriately within the figure. Do have any suggestions on how can I fix that? Thanks
@anntzer On a completely different note, and since decomposing text position is hard: Would it be possible/interesting to render the text in black, extract that from the canvas as pixel image. Recolor the pixel image with a continuos gradient and write it back? - Of course, this is quite hacky, but so is the word decomposition.
Really not so much. The implementation is basically
frompylabimport*t0=text(.1, .1, "hello", c="C0")
t1=annotate(
"world",
xy=(1, 0), xycoords=t0, # Immediately to the left of t0.xytext=(.5, 0), textcoords="offset fontsize", # Shifted by half a fontsize (for spacing).va="bottom", c="C1")
...
show()
One just needs to wrap this in a proper loop over the words and figure out the correct alignment flags for the vertical case.
Recolor the pixel image with a continuos gradient and write it back?
Isn‘t that enough then? A rainbow-by-word coloring doesn’t seem too useful. Maybe replace this example by one that colors single words in a text? (I assume that would also need the proposed annotate pattern)
I agree the rainbow-by-word example is not really useful in itself indeed (except for the fact that it produces selectable text in pdf output, which is a slightly niche requirement for the usecase here), but demo-ing positioning-relative-to-another artist (as in the implementation I suggest) is nice (IMO).
Perhaps this example could just include both approaches.
Activity
niranjank2022 commentedon May 21, 2023
I would like to work on this. Please assign me this task.
niranjank2022 commentedon May 21, 2023
I went through the Contributing Guidelines, it is mentioned that we can create PR and work on it. But, I have a doubt, which branch should I have to use for PR? Please help!
rcomer commentedon May 21, 2023
Thanks for your interest in contributing @niranjank2022! You should create a new branch to make the change. We have some workflow guidance here:
https://matplotlib.org/devdocs/devel/development_workflow.html
niranjank2022 commentedon May 21, 2023
Thank you. I will create a PR once I finish my changes, by tomorrow._
niranjank2022 commentedon May 23, 2023
One doubt!
We need plt.artist.Artist object to position each word next to each other, right?
Mubin17 commentedon May 26, 2023
@anntzer The code above is my attempt to rewrite rainbow_text simpler fashion using annotate(), however, I am having an issue formatting text appropriately within the figure. Do have any suggestions on how can I fix that? Thanks
anntzer commentedon May 26, 2023
I don't have the actual implementation written, figuring that out is the point of the issue.
timhoffm commentedon May 26, 2023
@anntzer On a completely different note, and since decomposing text position is hard: Would it be possible/interesting to render the text in black, extract that from the canvas as pixel image. Recolor the pixel image with a continuos gradient and write it back? - Of course, this is quite hacky, but so is the word decomposition.
anntzer commentedon May 27, 2023
Really not so much. The implementation is basically
One just needs to wrap this in a proper loop over the words and figure out the correct alignment flags for the vertical case.
That's more or less already showcased at https://matplotlib.org/stable/gallery/text_labels_and_annotations/demo_text_path.html.
timhoffm commentedon May 27, 2023
👍
Isn‘t that enough then? A rainbow-by-word coloring doesn’t seem too useful. Maybe replace this example by one that colors single words in a text? (I assume that would also need the proposed annotate pattern)
anntzer commentedon May 27, 2023
I agree the rainbow-by-word example is not really useful in itself indeed (except for the fact that it produces selectable text in pdf output, which is a slightly niche requirement for the usecase here), but demo-ing positioning-relative-to-another artist (as in the implementation I suggest) is nice (IMO).
Perhaps this example could just include both approaches.