Skip to content

Latest commit

 

History

History
525 lines (371 loc) · 15.6 KB

plot_text.md

File metadata and controls

525 lines (371 loc) · 15.6 KB

An Automatic Way of Adding Text in Matplotlib

  • If you need any help or have any suggestion/feedback ping me here.

Content

Overview

  • soccerplots package now comes with a utility function that can help users to add and modify text without the need of using text() method.

  • plot_text() is the method which can be used to add and modify text as per the users need.

  • Here, we will cover the documentation of plot_text() method with some examples to show how one can add and modify text.

soccerplots.utils.plot_text

soccerplots.utils.plot_text(x, y, text, text_dict, ax, color_rest='k', align="left", fontsize=None, **kwargs)
No. Parameter About Parameter
1. x (float) x-coodrinate value for text.
2. y (float) y-coodrinate value for text.
3. text (str) the text that will be added.
4. text_dict (dict) contains words that the user wants to format.
5. ax (ax.Axes) axis object.
6. color_rest (str, optional) color for the text. Defaults to 'k'.
7. align (str, optional) alignment of boxes. Defaults to "left".
8. fontsize (float, optional) size of the text. Default to None.
9. **kwargs(optional) all other keyword arguments are passed on to matplotlib.axes.Axes.text.
No. Returns About
1. ax (axes.Axes)axis object

Examples

Adding text with colors

  • Here, we will add a text saying Hello World!!! This is Python Programming and will color Python with green color and Programming with crimson color.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Hello World!!! This is Python Programming"

## addition info about text
text_dict = dict(
    Python = dict(color="green"),         ## Python color is "green"
    Programming = dict(color="crimson")   ## Programming color is "crimson"
)

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

1_simple_text

Add Fontstyle

  • We will add a text saying Hello World!!! Matplotlib is Love and will add italic fontstyle to Hello World!!! and color Matplotlib and Love.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Hello World!!! Matplotlib is Love"

## addition info about text
text_dict = {
    "Hello": dict(fontstyle="italic"),      ## Hello is italic
    "World!!!": dict(fontstyle="italic"),   ## World!!! is italic
    "Matplotlib": dict(color="#897941"),    ## color to Matplotlib
    "Love": dict(color="crimson")           ## color to Love
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

2_fontstyle

  • There is an alternative way of doing this.

  • Since we can see that in the above text Hello and World!!! are coming together. So without passing them as multiple keys we can pass them as a single key, for that we have to change the text a bit.

  • The text will now look like: Hello_World!!! Matplotlib is Love and then we will pass ignore=True to the dictionary for the Hello_World!!! key in text_dict.(see code-snippet below)

  • _(underscore) will tell the method that we are combining these two words and the modification will come for both these words, ignore=True will tell the method to ignore the _ sign in the text and replace it with (space).

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Hello_World!!! Matplotlib is Love"                      ## _ has been added

## addition info about text
text_dict = {
    "Hello_World!!!": dict(fontstyle="italic", ignore=True),    ## ignore_last has been set to True
    "Matplotlib": dict(color="#897941"),                        ## color to Matplotlib
    "Love": dict(color="crimson")                               ## color to Love
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

2_fontstyle_2

Coloring consecutive words

  • Our text here is the same as above Hello World!!! Matplotlib is Love and we will color Hello World!!! also making it italic.

  • Again, we will change the text a bit adding _ between Hello and World!!! and will pass ignore=True and some color-value to the dictionary.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Hello_World!!! Matplotlib is Love"       ## added underscore

## addition info about text
text_dict = {
    "Hello_World!!!": dict(fontstyle="italic", color="#3524A6", ignore=True),    ## added color 
    "Matplotlib": dict(color="#897941"),      ## color to Matplotlib
    "Love": dict(color="crimson")             ## color to Love
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

3_coloring_cons

Fontweight

  • The text is: Hello World!!! Matplotlib is Love.

  • Here we will add fontweight="bold" for Matplotlib and Love and Hello World!!! will remain italic and will add some color too.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Hello_World!!! Matplotlib is Love"

## addition info about text
text_dict = {
    "Hello_World!!!": dict(fontstyle="italic", color="#3524A6", ignore=True),    ## italic and color
    "Matplotlib": dict(color="#897941", fontweight="bold"),                      ## bold and color
    "Love": dict(color="crimson", fontweight="bold")                             ## bold and color
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

4_bold

Changing size of words

  • Here we will see how to change the size of words in a text.

  • Our text is This word is small and this word is large and we will add color to word and make it bold and change fontsize for small and large.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "This word is small and this word is large"

## addition info about text
text_dict = {
    "word": dict(fontweight="bold", color="#897941"),    ## bold and color
    "small": dict(fontsize="small"),                     ## fontsize is "small", can pass a floating value as well
    "large": dict(fontsize="large")                      ## fontsize is "large", can pass a floating value as well
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

4_small

Changing fontsize of the whole text

  • Our text here is: Computer Programming is just awesome

  • And we will increase the fontsize for our whole string also adding color to awesome and making Computer Programming italic.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Computer_programming is just awesome"

## addition info about text
text_dict = {
    "awesome": dict(fontweight="bold", color="#897941"),            ## bold with color
    "Computer_programming": dict(fontstyle="italic", ignore=True)   ## italic 
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax, fontsize=15       ## increased fontsize
)
  • Output:

4_small_fontfamily

Changing fontstyle

  • We will now see how to change fontstyle, the text is the same as above.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "Computer_programming is just awesome"

## addition info about text
text_dict = {
    "awesome": dict(fontweight="bold", color="#897941"),
    "Computer_programming": dict(fontstyle="italic", ignore=True)
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax, fontfamily="Purisa"        ## added fontstyle
)
  • Output:

4_small_fontsize

Add multiple lines with colors and styles

  • Here we will add two lines:
This is Line Number 01
And This is Line Number 02
  • We will add color, italic fontstyle and bold fontweight to Number 01, and will add color, oblique fontstyle and roman fontweight to Number 02.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = "This is Line Number_01\nAnd This is Line Number_02"

## addition info about text
text_dict = {
    "Number_01": dict(color="crimson", fontstyle="italic", fontweight="bold", ignore=True),     ## added modifications
    "Number_02": dict(color="#897941", fontstyle="oblique", fontweight="roman", ignore=True)    ## added modifications
}

## create subplot
fig, ax = plt.subplots()

## plot the text
ax = plot_text(
    x = 0.5, y = 0.5, 
    text = text, text_dict = text_dict, ax = ax
)
  • Output:

5_mul_lines

  • Note: You can pass text for multiple line strings like this as well:
text = """This is Line Number_01
And This is Line Number_02"""

ignore_last

  • Let's say we have Corners, Crosses, Freekicks and Passes as our text and we want to color Corners and Crosses but we don't want to color the , that are included in those words, what we will do is pass another argument ignore_last=True, this will ignore the last character of a word and make the required changes.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = """How are the best creators in Europe's top 5 league making chances
through Corners, Crosses, Indirect_Freekicks and Open_Play_Passes?"""

## addition info about text
## ignore_last will ignore the last word
text_dict = {
    "Corners,": dict(color="skyblue", ignore_last=True),
    "Crosses,": dict(color="gold", ignore_last=True),                    
    "Indirect_Freekicks": dict(color="grey", ignore=True),
    "Open_Play_Passes?": dict(color="crimson", ignore=True, ignore_last=True)
}

## create subplot
fig, ax = plt.subplots(facecolor="#121212")
ax.set_facecolor("#121212")
ax.set(xlim=(-2, 2))
ax.axis("off")

## plot the text
ax = plot_text(
    x = 0., y = 0.6, 
    text = text, text_dict = text_dict, ax = ax, 
    color_rest="#F0FFF0", fontsize=9
)
  • Output:

6_foot_1

Different fontsize for different lines

  • Let's say we have the following text:
text = """La Liga 2019/20: Expected Performance
Rolling average comparison between goal difference & expected goal difference"""
  • And now we want to have larger fontsize for the first line and a smaller one for the second. We can do this by passing a list of fontsizes to plot_text method.

  • Code Snippet:

import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## text to be plotted
text = """La Liga 2019/20: Expected Performance
Rolling average comparison between goal_difference & expected_goal_difference"""

## text dict
text_dict = {
    "goal_difference": dict(color="skyblue", ignore=True),
    "expected_goal_difference": dict(color="gold", ignore=True)
}

## create subplot
fig, ax = plt.subplots(facecolor="#121212")
ax.set_facecolor("#121212")
ax.set(xlim=(-2, 2))
ax.axis("off")

## plot the text
ax = plot_text(
    x = 0., y = 0.6, 
    text = text, text_dict = text_dict, ax = ax, 
    color_rest="#F0FFF0", fontsize=[13, 9], va="top"     ## list of fontsize and 
                                                         ## va="top" to reduce distance between first and second line
)
  • Output:

6_foot_4

Quote

  • Code Snippet:
import matplotlib.pyplot as plt
from soccerplots.utils import plot_text

## file at http:https://bit.do/johantxt
with open("johan.txt", "r") as ofile:
    text = ofile.read()

text_dict = {
    "youth_teams": dict(color="#00FF00", fontstyle="italic", ignore=True),
    "first_team.": dict(color="#F5F5F5", fontstyle="italic", 
                       fontweight="semibold", ignore=True, ignore_last=True),
    "emhasis": dict(color="gold", fontweight="demibold"),
    "learning.": dict(color="#00FFFF", ignore_last=True),
    "suspicion": dict(color="#F04D4D", fontweight="roman"),
    "youth_coaches": dict(color="#00FF00", fontstyle="italic", ignore=True),
    "winning.": dict(color="crimson", ignore_last=True),
    "reputation.": dict(color="#008080", fontweight="bold", ignore_last=True),
    "club.": dict(color="gold", ignore_last=True),
    "talent": dict(color="#C674C6"),
    "learn,": dict(color="#00FFFF", fontweight="bold", ignore_last=True),
    "point?": dict(color="lime", ignore_last=True),
    "developing": dict(color="#32CD32"),
    "Johan_Cruyff": dict(color="#FFA500", ignore=True, fontsize=15)
}

## create subplot
fig, ax = plt.subplots(figsize=(12, 4), facecolor="#121212")
ax.set_facecolor("#121212")
ax.axis("off")

ax = plot_text(0.5, 0.5, text, text_dict, ax=ax, color_rest="#FFFAFA", 
               fontsize=12, fontfamily="Norasi")
  • Output:

quote