Matplotlib string with italics and using .format()

Question:

I can make italic text like this:

plt.title('$it{Text}$')
plt.show()

I’d like to have italic text and use the .format() function, e.g.:

plt.title('$it{Text}$ = {}'.format(func))
plt.show()

This returns KeyError: 'Text'

Asked By: Cactus Philosopher

||

Answers:

Since matplotlib.pyplot.title accepts style as a keyword argument, you can pass italic :

import matplotlib.pyplot as plt

text = "Stack Overflow"

plt.title(f"{text}", style= "italic")
#or plt.title("{}".format(text), style= "italic")

plt.show()

enter image description here

Or if only some of the desired text is to be italic:

text = "italic" 
text2 = "something" 

plt.title(f"$it{text}$ ~ {text2}")
Answered By: abokey

I Hope I understood your question.

you can check this web if you wanna know more about format function: https://www.geeksforgeeks.org/python-string-format-method/

import matplotlib.pyplot as plt

s=[11,22,33,44,55,66]
a=[22,33,44,22,1,34]

plt.plot(a,s)
plt.title("{}".format("GLITCH"),style="italic")
plt.show()
Answered By: Abhay
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.