0

I have the following code here:

import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import matplotlib.colors
from simple-colors import * 

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:

enter image description here

I would like to add text to this figure:

fig.text(0.3,0.05,'Pearsons Correlation Coefficient is {}'.format(Correlation))

This gives me the output:

enter image description here

However, I want to make part 'Pearsons Correlation Coefficient' bold and underlined.

I have tried:

fig.text(0.3,0.05,"Pearson's Correlation Coefficient is: " + r"$\bf\underline{" + str(Correlation) + "}$")

But this gives me the error:

RuntimeError: Failed to process string with tex because latex could not be found

Could anyone give me a helping hand?

Caledonian26
  • 592
  • 5
  • 17
  • 1
    Try [this](https://stackoverflow.com/questions/10727368/underlining-text-in-python-matplotlib) and [this](https://stackoverflow.com/questions/34937048/make-part-of-a-matplotlib-title-bold-and-a-different-color) – Sheldore Jun 12 '20 at 12:38
  • do you have the latex executable installed on your system? – Paul H Jun 12 '20 at 13:41
  • is that visible on your path? can you open up a terminal and compile a latex document? what does pycharm have to do with this? if you're using it, i wouldn't trouble shoot this issue outside of it in a plain terminal – Paul H Jun 12 '20 at 13:46
  • @Caledonian26 I don't think you've characterized that correctly. https://matplotlib.org/tutorials/text/usetex.html?highlight=latex – Paul H Jun 12 '20 at 14:27
  • @PaulH see my answer below! – Caledonian26 Jun 13 '20 at 23:15

1 Answers1

-1

It seems that you are not just using "plain" Matplotlib, but you're using it embedded on Latex. Therefore, the error is not related to Matplotlib per se, but rather is related to Latex. Please have a look at the solution here