Add a box with a comment to a plot

Viewed 40

I am not really experienced with plots in python, but as I want to increase the quality of my data illustration and analysis, I am trying to use python more.

Example-Plot

In the attached picture, you can see my plot, which shows some elements and how they change. As you can see in the picture, I would like to have some kind of box, in a transparent color with a comment, that highlights the starting points of the curves. For the example, I added the box with PowerPoint. Can you please tell me, how this could be done in python?

The code looks like this:

import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.pyplot import figure
  

x = [2.5,5,10,15,20,25,30,35,40,45,50,52.5]
C = [0.08,0.07,0.07,0.07,0.07,0.02,0.02,0.02,0.02,0.02,0.02,0.01]
Si = [0.9,0.9,0.9,0.8,0.7,0.6,0.6,0.4,0.5,0.5,0.4,0.65]
Mn = [7,6,4,5,5,5,6,4,3,2,1,0.7]
Cr = [19.2,18,17,16,16,16,17,14,13,12.5,12,12.2]
Ni = [9,8,8,8,7,5,4,6,5,4,4,4.8]
Mo = [0,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.5,0.5]

plt.plot(x, C,  color='k', label='C')
plt.plot(x, Si, color='b', label='Si')
plt.plot(x, Mn, color='g', label='Mn')
plt.plot(x, Cr, color='r', label='Cr')
plt.plot(x, Ni, color='c', label='Ni')
plt.plot(x, Mo, color='m', label='Mo')
 
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.title("my plot")
plt.legend()
  
plt.show()

I know that this is fairly simple, but was not able to find a helpful answer here of elsewhere.

I am thankful for every suggestion.

Best regards

Bene

1 Answers

You would need to use matplotlib's pyplot.text function with the "rotation" argument to flip your textbox vertically.

Heres an example using your code:

import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.pyplot import figure
  

x = [2.5,5,10,15,20,25,30,35,40,45,50,52.5]
C = [0.08,0.07,0.07,0.07,0.07,0.02,0.02,0.02,0.02,0.02,0.02,0.01]
Si = [0.9,0.9,0.9,0.8,0.7,0.6,0.6,0.4,0.5,0.5,0.4,0.65]
Mn = [7,6,4,5,5,5,6,4,3,2,1,0.7]
Cr = [19.2,18,17,16,16,16,17,14,13,12.5,12,12.2]
Ni = [9,8,8,8,7,5,4,6,5,4,4,4.8]
Mo = [0,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.5,0.5]

plt.plot(x, C,  color='k', label='C')
plt.plot(x, Si, color='b', label='Si')
plt.plot(x, Mn, color='g', label='Mn')
plt.plot(x, Cr, color='r', label='Cr')
plt.plot(x, Ni, color='c', label='Ni')
plt.plot(x, Mo, color='m', label='Mo')
 
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.title("my plot")
plt.legend()
  
plt.text(2,8, "comment", bbox={'facecolor': 'dimgray', 'alpha': 0.30, 'pad': 10} , rotation = 90, )
plt.show()

Which produces the result:

example picture

The height of this verticle box will be determined by the length of your text. You could fill it up with space characters to make it however long you want.

Related