I am trying to create a graph using arrays populated by variables each of witch has a value but when I run the code the graph shows up with no data (plotted lines). It does however have the correct axis labels. It has 2 y axis however the code is almost identical to what I have used previously that worked for a traditional (x, y) plot. When it failed originally I added x, y limits to see if that would help. Here is what I have:
fig, ax1 = plt.subplots()
color = 'tab:blue'
ax1.plot = (array_Wnet_22, array_homes_22)
ax1.set_xlim((0, 5000))
ax1.set_ylim((0, 4000))
ax1.set_xlabel('Net Power (kW)')
ax1.set_ylabel('Number of homes served', color = color)
ax1.tick_params(axis='y', labelcolor = color)
ax2 = ax1.twinx()
color = 'tab:orange'
ax2.plot = (array_Wnet_22, array_t_22)
ax2.set_xlim((0,5000))
ax2.set_ylim((0,600))
ax2.set_ylabel(' Peak Turbine Temperature (K)', color = color)
ax2.tick_params(axis='y', labelcolor = color)
plt.show()
(edit): this is the current output of the graph
The problem parameters are to create a double axis graph of net power output, how many homes it will serve, and peak temperatures of/ within a steam turbine given mass flow, low and high pressures, and steam quality. The issue I am having is that none of my data is being plotted however I know that each variable within the array has a set value.
(edit#2): Here is what the code looks like that populates my arrays. I know its clunky and big and there's a better way to do it but I'm new to python. I am using pyromat, numpy, and matplot lib together to make it work. Some of the variables are being recalled from a previous problem I can add the code for those as well if need be.
T31_22 = h20.T(p=1723, h=h31_21)
T32_22 = h20.T(p=2223, h=h32_21)
T33_22 = h20.T(p=2723, h=h33_21)
T34_22 = h20.T(p=3223, h=h34_21)
T35_22 = h20.T(p=3723, h=h35_21)
T36_22 = h20.T(p=4223, h=h36_21)
T37_22 = h20.T(p=4723, h=h37_21)
T38_22 = h20.T(p=5000, h=h38_21)
array_t_22 = [T31_22, T32_22, T33_22, T34_22, T35_22, T36_22, T37_22, T38_22]
Wp1_22 = (8.2*(h11_21-h21_21))
Wp2_22 = (8.2*(h12_21-h22_21))
Wp3_22 = (8.2*(h13_21-h23_21))
Wp4_22 = (8.2*(h14_21-h24_21))
Wp5_22 = (8.2*(h15_21-h25_21))
Wp6_22 = (8.2*(h16_21-h26_21))
Wp7_22 = (8.2*(h17_21-h27_21))
Wp8_22 = (8.2*(h18_21-h28_21))
array_Wp = [Wp1_22, Wp2_22, Wp3_22, Wp4_22, Wp5_22, Wp6_22, Wp7_22, Wp8_22]
Wt1_22 = (8.2*(h31_21-h41_21))
Wt2_22 = (8.2*(h32_21-h42_21))
Wt3_22 = (8.2*(h33_21-h43_21))
Wt4_22 = (8.2*(h34_21-h44_21))
Wt5_22 = (8.2*(h35_21-h45_21))
Wt6_22 = (8.2*(h36_21-h46_21))
Wt7_22 = (8.2*(h37_21-h47_21))
Wt8_22 = (8.2*(h38_21-h48_21))
array_Wt = [Wt1_22, Wt2_22, Wt3_22, Wt4_22, Wt5_22, Wt6_22, Wt6_22, Wt7_22, Wt8_22]
Wnet1_22 = Wt1_22 Wp1_22
Wnet2_22 = Wt2_22 Wp2_22
Wnet3_22 = Wt3_22 Wp3_22
Wnet4_22 = Wt4_22 Wp4_22
Wnet5_22 = Wt5_22 Wp5_22
Wnet6_22 = Wt6_22 Wp6_22
Wnet7_22 = Wt7_22 Wp7_22
Wnet8_22 = Wt8_22 Wp8_22
array_Wnet_22 = [Wnet1_22, Wnet2_22, Wnet3_22, Wnet4_22,
Wnet5_22, Wnet6_22, Wnet7_22, Wnet8_22]
array_homes_22 = [0, 650, 1300, 1950, 2600, 3250, 3900, 4000]
(edit#3): The graph should look smooth as all numbers are increasing with net power. Again, I am very new to python and not sure what I did wrong here.
(edit#4): Somehow something worked, I'm not sure what but here is the final code and what it ended up looking like. Thank you everyone for your help.
fig, ax1 = plt.subplots()
color = 'tab:blue'
ax1.plot(array_Wnet_22, array_homes_22)
ax1.set_xlabel('Net Power (kW)', color=color)
ax1.set_ylabel('Number of homes served', color = color)
ax1.tick_params(axis='y', labelcolor = color)
ax2 = ax1.twinx()
color = 'tab:orange'
ax2.plot(array_Wnet_22, array_t_22, color = color)
ax2.set_ylabel(' Peak Turbine Temperature (K)', color = color)
ax2.tick_params(axis='y', labelcolor = color)
plt.show()
CodePudding user response:
It will be better to provide a sample of the two dataset you are trying to plot. The data must not be necessarily what you are using otherwise there is no much help that can be provided.
CodePudding user response:
Your main problem is that you are not calling the plot
function.
In addition to this, make sure your data is actually in the range that your plot is being limited.
This is the fixed code, with some improvements (pay attention to comments):
import matplotlib.pylab as plt
import numpy as np
np.random.seed(0) # make this snippet reproducible
# generate synthetic data
array_Wnet_22 = np.arange(11) * 500
# make sure those are in apropiated range, since your code limits them
array_homes_22 = 2500 np.random.uniform(-1000,1000, size=11)
array_t_22 = 300 np.random.uniform(-150,150, size=11)
fig, ax1 = plt.subplots()
color = 'tab:blue'
# main change in following line: actually call the plot function
# also add `color=color` to let the result plot with same color as labels
ax1.plot(array_Wnet_22, array_homes_22, color=color)
ax1.set_xlim((0, 5000))
ax1.set_ylim((0, 4000))
ax1.set_xlabel('Net Power (kW)')
ax1.set_ylabel('Number of homes served', color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx()
color = 'tab:orange'
# again: main change in following line: actually call the plot function
# also add `color=color` to let the result plot with same color as
ax2.plot(array_Wnet_22, array_t_22, color=color)
ax2.set_xlim((0,5000))
ax2.set_ylim((0,600))
ax2.set_ylabel(' Peak Turbine Temperature (K)', color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.show()
which outputs the following plot:
CodePudding user response:
I'm unsure of what I changed exactly other than calling the plots to function which gave me an error message before, but it works now, here is the final code I used.
fig, ax1 = plt.subplots()
color = 'tab:blue'
ax1.plot(array_Wnet_22, array_homes_22)
ax1.set_xlabel('Net Power (kW)', color=color)
ax1.set_ylabel('Number of homes served', color = color)
ax1.tick_params(axis='y', labelcolor = color)
ax2 = ax1.twinx()
color = 'tab:orange'
ax2.plot(array_Wnet_22, array_t_22, color = color)
ax2.set_ylabel(' Peak Turbine Temperature (K)', color = color)
ax2.tick_params(axis='y', labelcolor = color)
plt.show()