#time = df["时间(hh:mm:ss)"] #将XX:XX:XX转换为min time = df["时间(hh:mm:ss)"] time_diff_mins = [0] t = datetime.strptime(df["时间(hh:mm:ss)"][0] , "%H:%M:%S")#起始 for i in range(1,len(time)): t1 = datetime.strptime(df["时间(hh:mm:ss)"][i] , "%H:%M:%S") time_diff = t1 - t#时间增量 time_diff_mins.append(round(time_diff.total_seconds()/60 , 2))#保留2位小数 #分别对分钟、油压、砂比、总排量赋值 p1 = np.array(time_diff_mins) p2 = np.array(df["油压(MPa)"]) p3 = np.array(df["砂比(%)"]) p4 = np.array(df["总排量(m^3)"]) fig , ax = plt.subplots(figsize=(8,4) , constrained_layout=True) ax.set_xlabel("Time(min)") ax.set_ylabel("Pressure(MPa)",color="blue") ax.set_xlim([0,120]) ax.set_ylim([0,120]) ax.tick_params(axis="y" , colors="blue") #创建共享x轴的twin1,twin2 twin1 = ax.twinx() twin2 = ax.twinx() ax.spines["right"].set_color("none") twin1.set_ylabel("Proppant conc(%)" , color="orange") twin1.set_ylim([0,80]) #修改坐标轴twin1刻度的颜色 twin1.tick_params(axis="y" , colors="orange") #确定twin2轴右边轴的位置为140 twin2.spines["right"].set_position(("data",140)) twin2.set_ylabel("Pume rate(m3/min)",color="g") twin2.set_ylim([0,40]) #修改坐标轴twin2刻度的颜色 twin2.tick_params(axis="y" , colors="green") #显示图例,对参数命名时加逗号,否则报错 z1, = ax.plot(p1 , p2 , linestyle="-" , color="blue" , label="Pressure(MPa)") z2, = twin1.plot(p1 , p3 , linestyle="-" , color="orange" , label="Proppant conc(%)") z3, = twin2.plot(p1 , p4 , linestyle="-" , color="green" , label="Pume rate(m3/min)") ax.legend(handles=[z1,z2,z3] , loc="upper left")
时间: 2023-11-06 09:08:30 浏览: 47
To extract the data where the proppant concentration is greater than zero and highlight the corresponding region in the diagram using matplotlib, you can follow these steps:
1. Filter the data based on the condition where the proppant concentration is greater than zero.
2. Use matplotlib to create a figure and axes.
3. Set the x-axis label as "Time (min)" and the y-axis label for the pressure as "Pressure (MPa)" (in blue color).
4. Set the limits for the x-axis (0 to 120) and y-axis for the pressure (0 to 120).
5. Create a twin axes (twin1) on the right side and set its y-axis label as "Proppant conc (%)" (in orange color) and limits for the y-axis (0 to 80).
6. Create another twin axes (twin2) on the right side and set its y-axis label as "Pump rate (m3/min)" (in green color) and limits for the y-axis (0 to 40).
7. Plot the data for pressure, proppant concentration, and pump rate on their respective axes using the plot function.
8. Set different colors and line styles for each plot.
9. Add legends for each plot.
10. Display the legend in the upper left corner of the graph.
Please note that you need to have the necessary data available in numpy arrays (p1, p2, p3, p4) before executing these steps.