matplotlib Python:为单个图形中的不同图获取正确的颜色

rryofs0p  于 2023-08-06  发布在  Python
关注(0)|答案(1)|浏览(117)

我很难在我的图表中为不同的图获得不同颜色的线,我无法在网上找到解决方案。
下面是当前的数字,指出了线路的问题。也就是说,每一行都应该是一种颜色,但它们在中间切换。


的数据

import math
import matplotlib.pyplot as plt

# Constants
length = 1.0  # Length of the tube in meters
diameter = 10 / 1000  # Diameter of the tube in meters
emissivity = 0.73  # Emissivity of the tube
outside_temp_list = [-40, -20, 0, 20, 40, 60, 70]  # Outside temperatures in °C
outside_pressure = 1 * 10**5  # Outside pressure in Pascal
initial_pressure = 10 * 10**5  # Initial pressure of hydrogen inside the tube in Pascal
initial_temp = 25 + 273  # Initial temperature of hydrogen inside the tube in Kelvin
Cv = 22  # Specific heat capacity of hydrogen at 10 bars and 25 °C in J/(mol·K)
k = 0.10  # Thermal conductivity of hydrogen at 10 bars and 25 °C in W/(m·K)

# Calculate area and volume of the tube
radius = diameter / 2
area = 2 * math.pi * radius * length
volume = math.pi * radius * radius * length

# Calculate number of moles of hydrogen inside the tube
R = 8.314  # Universal gas constant in J/(mol·K)
n = initial_pressure * volume / (R * initial_temp)

# Create lists to store the results
time_list = []
temperature_list = []
pressure_list = []

# Perform the calculations for each outside temperature scenario
for outside_temp in outside_temp_list:
    time = 0
    temperature = initial_temp
    pressure = initial_pressure
    
    # Update k and Cv for specific temperatures
    if outside_temp == 40:
        k = 0.12
        Cv = 25
    elif outside_temp == 60:
        k = 0.2
        Cv = 26
    elif outside_temp == 70:
        k = 0.3
        Cv = 27
    
    while abs(temperature - (outside_temp + 273)) > 0.001:
        time_list.append(time)
        temperature_list.append(temperature)
        pressure_list.append(pressure)
    
        dt = 1
    
        # Calculate the rate of heat transfer by radiation
        sigma = 5.67e-8
        Q_rad = -emissivity * sigma * area * (temperature**4 - (outside_temp + 273)**4) * dt
    
        # Calculate the rate of heat transfer by conduction
        Q_cond = -k * area * (temperature - (outside_temp + 273)) * dt / length
    
        # Update the temperature and pressure
        temperature += (Q_rad + Q_cond) / (n * Cv)
        pressure = (n * R * temperature) / volume
    
        time += dt

# Generate the table
table = zip(time_list, temperature_list, pressure_list)
print("Time (sec)\tTemperature (K)\tPressure (Pa)")
for row in table:
    print(f"{row[0]}\t\t{row[1]:.2f}\t\t{row[2]:.2f}")

# Convert pressure from Pascal to bar
pressure_list_bar = [p / 100000 for p in pressure_list]

# Plot the results
plt.plot(pressure_list_bar[:len(time_list)//7], time_list[:len(time_list)//7], label="-40 °C", color='blue')
plt.plot(pressure_list_bar[len(time_list)//7:2*len(time_list)//7], time_list[len(time_list)//7:2*len(time_list)//7], label="-20 °C", color='red')
plt.plot(pressure_list_bar[2*len(time_list)//7:3*len(time_list)//7], time_list[2*len(time_list)//7:3*len(time_list)//7], label="0 °C", color='green')
plt.plot(pressure_list_bar[3*len(time_list)//7:4*len(time_list)//7], time_list[3*len(time_list)//7:4*len(time_list)//7], label="+20 °C", color='purple')
plt.plot(pressure_list_bar[4*len(time_list)//7:5*len(time_list)//7], time_list[4*len(time_list)//7:5*len(time_list)//7], label="+40 °C", color='orange')
plt.plot(pressure_list_bar[5*len(time_list)//7:6*len(time_list)//7], time_list[5*len(time_list)//7:6*len(time_list)//7], label="+60 °C", color='yellow')
plt.plot(pressure_list_bar[6*len(time_list)//7:], time_list[6*len(time_list)//7:], label="+70 °C", color='brown')

plt.title("Pressure vs Time")
plt.xlabel("Pressure (bar)")
plt.ylabel("Time (sec)")
plt.legend()
plt.grid(True)
plt.show()

字符串

yxyvkwin

yxyvkwin1#

你的问题是你假设所有的线都有相同的点数,而实际上并非如此。因为我们知道每一行都从时间0开始,所以我们可以循环遍历时间列表并保存时间为0的点。现在,我们知道对于给定的索引,这是时间跨度的开始,下一个索引是下一个索引的开始。因此,我们可以使用每两个索引对数据进行切片。一个简单的循环方法是zip从开始到倒数第二个元素的列表,相同的列表从第二个元素开始。我们还需要考虑最后一个时间跨度,因此我们可以将-1附加到列表中,因为-1索引了最后一个元素。
我还做了另外两个改变。
1.我把标签和颜色列成清单。
1.我交换了坐标轴,将时间(自变量)放在x轴上,将压力(因变量)放在y轴上,因为这是惯例。

time_resets = [i for i, time in enumerate(time_list) if time == 0]
time_resets.append(-1)
temperatures = ["-40 °C", "-20 °C", "0 °C", "+20 °C", "+40 °C", "+60 °C", "+70 °C"]
colors = ["blue", "red", "green", "purple", "orange", "yellow", "brown"]

for start, stop, temperature, color in zip(time_resets[:-1],
                                           time_resets[1:],
                                           temperatures,
                                           colors):
    plt.plot(time_list[start:stop], pressure_list_bar[start:stop],
             label=temperature, color=color)

plt.xlabel("Time (sec)")
plt.ylabel("Pressure (bar)")
plt.title("Pressure vs Time")
plt.legend()
plt.grid()
plt.show()

字符串
x1c 0d1x的数据

相关问题