1

I have the code below:

fig, ax = plt.subplots(1, 2, figsize = (8, 8))
ax[0].set_title('Deaths from Cancer in Michigan 2020',pad=13,fontweight='bold',color='navy')
ax[1].set_title('Deaths from COVID-19 in Michigan 2020',pad=13,fontweight='bold',color='navy')
ax[0].set_ylabel('Number of cases',labelpad=6,color='brown')
ax[1].set_ylabel('Number of cases',labelpad=6,color='brown')
ax[0].set_xlabel('Month',labelpad=6,color='brown')
ax[1].set_xlabel('Month',labelpad=6,color='brown')
ax[0].plot(COVID19['month'], COVID19['Cases'], color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
ax[1].plot(Deaths['month'], Deaths['Cases'], color='red', marker='o', linestyle='dashed',linewidth=2, markersize=12)
ax[0].tick_params(axis='x', labelsize=10,rotation=45)
ax[1].tick_params(axis='x', labelsize=10,rotation=45)
ax[0].tick_params(axis='y', labelsize=10)
ax[1].tick_params(axis='y', labelsize=10)
Deaths = Deaths['Cases'].reset_index()
Deaths = Deaths.drop(['index'],axis=1)
fig.subplots_adjust(bottom=0.2)
Correlation = Deaths['Cases'].corr(COVID19['Cases'],method='pearson')

This gives me the output below:

enter image description here

I then try to annotate the figure:

class color:
       PURPLE = '\033[95m'
       CYAN = '\033[96m'
       DARKCYAN = '\033[36m'
       BLUE = '\033[94m'
       GREEN = '\033[92m'
       YELLOW = '\033[93m'
       RED = '\033[91m'
       BOLD = '\033[1m'
       UNDERLINE = '\033[4m'
       END = '\033[0;0m'
fig.text(0.3,0.05, (color.BOLD + "Pearson's Correlation Coefficient is: " + color.END + str(Correlation)))

However, I get the error: RuntimeWarning: Glyph 27 missing from current font. font.set_text(s, 0.0, flags=flags)

When I try the following:

class color:
       PURPLE = '\033[95m'
       CYAN = '\033[96m'
       DARKCYAN = '\033[36m'
       BLUE = '\033[94m'
       GREEN = '\033[92m'
       YELLOW = '\033[93m'
       RED = '\033[91m'
       BOLD = '\033[1m'
       UNDERLINE = '\033[4m'
       END = '\033[0;0m'
print(color.BOLD + color.UNDERLINE + "Pearson's Correlation Coefficient is: " + color.END)

This gives me:

enter image description here

Thus, I am able to make the text bold and underline it.

However, I am unable to do this in the context of my command above (annotate the figure with this string).

Could anyone give me a helping hand?

Caledonian26
  • 727
  • 1
  • 10
  • 27
  • There’s no reason that SGR codes would work in a graphical text display. – Davis Herring Jun 12 '20 at 19:52
  • Does this answer your question? [Underlining Text in Python/Matplotlib](https://stackoverflow.com/questions/10727368/underlining-text-in-python-matplotlib) and you can select the color of the text using the `color` or `c` parameter. You'll need to use the proper matplotlib API methods and parameters. – Trenton McKinney Jun 12 '20 at 20:39
  • 1
    @TrentonMcKinney actually dw! AMAZINGLY it works! I just tried it! thanks so much for your help! Will post answer below! – Caledonian26 Jun 13 '20 at 22:26

1 Answers1

0

Install the package MacTex here: https://www.tug.org/mactex/ (I was using a mac).

Once I had installed the package on my computer, I didn't need to change any of the settings on my python interpreter - I just left it as it was.

Then use the following command:

rc(‘text’,usetex=True)
fig.text(0.3,0.05, r"$\bf\underline{Pearsons \ Correlation \ Coefficient \ is:}$" + str(Correlation))

This will both underline and bolden the text 'Pearsons Correlation Coefficient is:'.

Adding a '\' with a space on either side between words will add a space between the words.

Caledonian26
  • 727
  • 1
  • 10
  • 27