0

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?

  • Have you tried to adapt [these suggestions](https://stackoverflow.com/questions/19385639/duplicate-items-in-legend-in-matplotlib)? – Mr. T May 17 '21 at 10:27
  • @Mr.T I tried, to no avail. What am I doing wrong? I added this line before else: plt.plot(Dk, Tsat_theor, label=Tcwin if Tcwin<35 else "") # ---------------> this is the code for plotting – Pavel Tashkinov May 17 '21 at 10:30
  • I cannot reproduce your problem - it is not a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). But as far as I understand your code, Tcwin will always be <35, so you don't really restrict anything. I also don't fully understand the code - it seems to loop twice with `for Tcwin in np.arange(17,35,3): while Tcwin < 35:` so each value 20, 23, 26,... will be executed multiple times. This might be intended, I don't know because it is not a reproducible example code. – Mr. T May 17 '21 at 16:01
  • @Mr.T I see. I tweaked it a little bit. Basically, I want to plot psat_theor=f(Dk) at different Tcwin in range(17,31,2) – Pavel Tashkinov May 17 '21 at 16:14
  • Err, that cannot be correct. You plot one data point with plt.plot(), so nothing shows up in the graph, let alone multiple legend entries. Please take your time to create an example code that reproduces your problem. I follow this question, so I will see when you update it with new code. – Mr. T May 17 '21 at 16:21
  • It seems you answer your initial question by manually creating the legend entries now. This is a valid method, and I consider this problem solved. – Mr. T May 18 '21 at 08:08

0 Answers0