I've got a code like this.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pyXSteam.XSteam
from pyXSteam.XSteam import XSteam
sns.set_style("ticks",{'grid.linestyle': '--'})
sns.color_palette("Spectral", as_cmap=True)
steamTable = XSteam(XSteam.UNIT_SYSTEM_MKS) # m/kg/sec/°C/bar/W
A = 3000 #define the heat exchange area
d_in = 20 #define the inner diameter of HE tubes
CF = 0.85 #define the cleanliness factor of SC.
w = 2.26 #define the water velocity within the tubes
Gw = 13000 #define the flow of CW, t/hr
e = 2.718281828
def Cp():
return steamTable.CpL_t(Tcwin)
def Ts():
return np.vectorize(Tsat_theor)
def Psat_theor():
return 100*steamTable.psat_t(Tsat_theor)
X = np.arange(27.418,301.598,27.418)
for Tcwin in np.arange(17,31,2):
Y = []
for Dk in X:
dk = (Dk * 1000 / (A * 3.600)) #calculate the relative steam load
K = CF * 4070 * ((1.1 * w / (d_in ** 0.25)) ** (0.12 * CF * (1 + 0.15 * Tcwin))) * (1 - (((35 - Tcwin) ** 2) * (0.52 - 0.0072 * dk) * (CF ** 0.5)) / 1000)
n = (K * A) / (Cp() * Gw * 1000)
Tcwout_theor = Tcwin + (Dk * 2225 / (Cp() * Gw))
Subcooling_theor = (Tcwout_theor - Tcwin) / (e ** (K * A / (Cp() * (Gw * 1000 / 3600) * 1000)))
TR_theor = (Tcwout_theor - Tcwin)
Tsat_theor = (Tcwout_theor + Subcooling_theor)
Y.append(Psat_theor())
plt.plot(X, Y)
plt.grid(True,which="both",ls="--",c='gray')
plt.legend([r'$t_{1в}$' + '=17 \N{DEGREE SIGN}С',
r'$t_{1в}$' + '=19 \N{DEGREE SIGN}С',
r'$t_{1в}$' + '=21 \N{DEGREE SIGN}С',
r'$t_{1в}$' + '=23 \N{DEGREE SIGN}С',
r'$t_{1в}$' + '=25 \N{DEGREE SIGN}С',
r'$t_{1в}$' + '=27 \N{DEGREE SIGN}С',
r'$t_{1в}$' + '=29 \N{DEGREE SIGN}С'])
plt.title(r'$P_{k}$' + '=f(' + '$D_{k}$' + ')')
sns.despine()
plt.show()
It plots well. Now I need to add the legend for each line this code creates. However, my attempt to do so by writing plt.legend() seems to have failed. Matplotlib does plot the legend, but it features more lines than it should. How can I bring Mpl to do this?