Seperate two strings in matplotlib legend

Viewed 49

This is the code I am working on :

import matplotlib.pyplot as plt
import numpy as np

T=[809,975,1086]

DeltaG1 = np.array([2,2.5,3])
DeltaG2=np.array([6,6.5,7])

yield_data = 500 + 500*np.random.rand(27)


legendstr=[rf'$\Delta G_1 = {x}  \Delta G_2 = {y} $' for x in DeltaG1 for y in DeltaG2]


for ii in range(9):
    
    
    idxs = [3*ii,3*ii+1,3*ii+2]
    Sim_yield=yield_data[idxs]


    plt.plot(T,Sim_yield)
    
    
   
plt.xlabel('X val')
plt.ylabel('Y val')
plt.legend(legendstr,bbox_to_anchor=[1,0.5],loc='right')

I I would like a space between the legend strings, for example, space between \DeltaG_1 =2.0 and \DeltaG_2=6.

I tried to give space like the one below, but did not work-

legendstr=[rf'$\Delta G_1 = {x}    \Delta G_2 = {y} $' for x in DeltaG1 for y in DeltaG2]

How do I separate the strings ?

1 Answers

You can use spaces escaped with backslash:

legendstr = [rf'$\Delta G_1 = {x}\ \ \ \ \Delta G_2 = {y}$' for x in DeltaG1 for y in DeltaG2]

Or as Johan said, split the latex into two parts:

legendstr = [rf'$\Delta G_1 = {x}$    $\Delta G_2 = {y}$' for x in DeltaG1 for y in DeltaG2]

legend with escaped spaces

Related