Issue
I am trying to plot two plots in one graph using matplotlib. Following is my code:
import matplotlib.pyplot as __plt
def plot(x, y, spec_x, spec_y):
if None in (x, y, spec_x, spec_y) or '' in (x, y, spec_x, spec_y):
raise ValueError
else:
__plt.xlim(min(spec_x), max(spec_x))
__plt.ylim(min(spec_y), max(spec_y))
__plt.plot(x, y, color='blue')
__plt.plot(spec_x, spec_y, color='red', linestyle='dashed')
__plt.xlabel('Frequencies (Hz)', fontsize=18)
__plt.ylabel('Y Label', fontsize=16)
__plt.xscale("log")
__plt.yscale("log")
__plt.grid(which='major', color='#DDDDDD', linewidth=0.8)
__plt.grid(which='minor', color='#EEEEEE', linestyle=':', linewidth=0.5)
__plt.savefig('test_graph.png', dpi=1000)
__plt.clf()
__plt.close()
if __name__ == "__main__":
x1_l1 = [0.0, 1e-07, 1.20226443461741e-07, 1.31825673855641e-07, 1.44543977074593e-07, 1.58489319246111e-07,
1.73780082874938e-07, 1.90546071796325e-07, 2.08929613085404e-07, 2.29086765276777e-07,
2.51188643150958e-07, 2.75422870333817e-07, 3.01995172040202e-07, 3.31131121482591e-07,
3.63078054770101e-07]
y1_l1 = [-0.887961849042741, -0.887961849042741, -0.887961849042949, -0.887961849043086, -0.887961849043252,
-0.887961849043453, -0.887961849043695, -0.887961849043987, -0.88796184904434, -0.887961849044765,
-0.887961849045278, -0.887961849045896, -0.887961849046641, -0.887961849047538, -0.887961849048619]
x1_l2 = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18,
0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 10.0]
y1_l2 = [0.638219, 0.321497, 0.216958, 0.165437, 0.135094, 0.115313, 0.101539, 0.0914968, 0.0839203, 0.078051,
0.073407, 0.0696679, 0.066613, 0.0640855, 0.0619714, 0.0601859, 0.0586651, 0.0573596, 0.0562311, 0.0552495,
0.0543905, 0.0536349, 0.052967, 0.0523738, 0.0518448, 0.0513711, 0.0509454, 0.0505615, 0.0502142, 0.049899, 1.908654]
print(len(x1_l1))
print(len(y1_l1))
print(len(x1_l2))
print(len(y1_l2))
x1_l2_e = []
x1_l1_e = []
for e in x1_l2:
x1_l2_e.append(format(e, 'f'))
for f in x1_l1:
x1_l1_e.append(format(f, 'f'))
plot(x1_l1_e, y1_l1, x1_l2_e, y1_l2)
I want the graph and its axis both to be in the range of x1_l2_e
and y1_l2
. I am setting up axis min and max using xlim
and ylim
, but the code is giving me an incorrect graph. Below is how it looks like:
I was expecting more like the below graph:
Any help?
PS - The original data list has large number of elements. I am posting just a few here.
Solution
Okay, so there are a couple of problems with the code.
First, your call to xlim
and ylim
are currently ignored. You are currently calling these before you actually plot anything. As such, the axis are set and then overwritten when you actually plot something. As such, moving these calls below the calls to plot will already get you closer.
Next, you convert the following
x1_l1 = [0.0, 1e-07, 1.20226443461741e-07, 1.31825673855641e-07, 1.44543977074593e-07, 1.58489319246111e-07,
1.73780082874938e-07, 1.90546071796325e-07, 2.08929613085404e-07, 2.29086765276777e-07,
2.51188643150958e-07, 2.75422870333817e-07, 3.01995172040202e-07, 3.31131121482591e-07,
3.63078054770101e-07]
x1_l1_e = []
for f in x1_l1:
x1_l1_e.append(format(f, 'f'))
We can make this a little easier by doing the following with list comprehension:
x1_l1_e = [0.0, 1e-07, 1.20226443461741e-07, 1.31825673855641e-07, 1.44543977074593e-07, 1.58489319246111e-07,
1.73780082874938e-07, 1.90546071796325e-07, 2.08929613085404e-07, 2.29086765276777e-07,
2.51188643150958e-07, 2.75422870333817e-07, 3.01995172040202e-07, 3.31131121482591e-07,
3.63078054770101e-07]
x = [format(x, 'f') for x in x1_l1_e]
However, if we do this you might notice that this only returns '0.0000'
because your values are so small. I would simply ignore this conversion entirely.
Next, you plot the y-axis as log with plt.yscale('log')
however your y values are all below 0. This typically does not work and can be seen from the error:
Data has no positive values, and therefore cannot be log-scaled.
Perhaps for your real data this is not a problem.
Combing all of this leads to the following code:
import matplotlib.pyplot as plt
x = [0.0, 1e-07, 1.20226443461741e-07, 1.31825673855641e-07, 1.44543977074593e-07, 1.58489319246111e-07,
1.73780082874938e-07, 1.90546071796325e-07, 2.08929613085404e-07, 2.29086765276777e-07,
2.51188643150958e-07, 2.75422870333817e-07, 3.01995172040202e-07, 3.31131121482591e-07,
3.63078054770101e-07]
y = [-0.887961849042741, -0.887961849042741, -0.887961849042949, -0.887961849043086, -0.887961849043252,
-0.887961849043453, -0.887961849043695, -0.887961849043987, -0.88796184904434, -0.887961849044765,
-0.887961849045278, -0.887961849045896, -0.887961849046641, -0.887961849047538, -0.887961849048619]
spec_x = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18,
0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 10.0]
spec_x = [format(x,'f') for x in x1_l2]
spec_y = [0.638219, 0.321497, 0.216958, 0.165437, 0.135094, 0.115313, 0.101539, 0.0914968, 0.0839203, 0.078051,
0.073407, 0.0696679, 0.066613, 0.0640855, 0.0619714, 0.0601859, 0.0586651, 0.0573596, 0.0562311, 0.0552495,
0.0543905, 0.0536349, 0.052967, 0.0523738, 0.0518448, 0.0513711, 0.0509454, 0.0505615, 0.0502142, 0.049899, 1.908654]
plt.plot(x, y, color='blue')
plt.plot(spec_x, spec_y, color='red', linestyle='dashed')
plt.xlabel('Frequencies (Hz)', fontsize=18)
plt.ylabel('Y Label', fontsize=16)
plt.xscale("log")
#plt.yscale("log")
plt.grid(which='major', color='#DDDDDD', linewidth=0.8)
plt.grid(which='minor', color='#EEEEEE', linestyle=':', linewidth=0.5)
# plt.xlim(min(spec_x), max(spec_x))
# plt.ylim(min(spec_y), max(spec_y))
Answered By - Mitchell van Zuylen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.