Python Forum
help with plt.fill_between
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with plt.fill_between
#1
Hi,

I want to write this script in a shorter way maybe using do loops.
Also in the way the script is written the instruction plt.fill_between is giving me
the following error: ValueError: Argument dimensions are incompatible

Any help? Also how do I attach a file to this message?

Maria

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator
from pylab import *
 
data_chi = np.loadtxt('energies.dat')
data_il7 = np.loadtxt('IL7.dat')
data_exp = np.loadtxt('exp.dat')
 
xx_chi = data_chi[:,0]
xx_il7 = data_il7[:,0]
xx_exp = data_exp[:,0]
 
xx_chi = xx_chi +3.
xx_il7 = xx_il7 +3. +0.1
xx_exp = xx_exp +3. +0.2
 
 
x_chi = [xx_chi,xx_chi+0.4]
x_il7 = [xx_il7,xx_il7+0.4]
x_exp = [xx_exp,xx_exp+0.4]
 
 
yy_chi_gs = data_chi[:,1]
ey_chi_gs = data_chi[:,2]
yy_il7_gs = data_il7[:,1]
ey_il7_gs = data_il7[:,2]
yy_exp_gs = data_exp[:,1]
ey_exp_gs = data_exp[:,2]
 
yy_chi_1s = data_chi[:,3]
ey_chi_1s = data_chi[:,4]
yy_il7_1s = data_il7[:,3]
ey_il7_1s = data_il7[:,4]
yy_exp_1s = data_exp[:,3]
ey_exp_1s = data_exp[:,4]
 
yy_chi_2s = data_chi[:,5]
ey_chi_2s = data_chi[:,6]
yy_il7_2s = data_il7[:,5]
ey_il7_2s = data_il7[:,6]
yy_exp_2s = data_exp[:,5]
ey_exp_2s = data_exp[:,6]
 
yy_chi_3s = data_chi[:,7]
ey_chi_3s = data_chi[:,8]
yy_il7_3s = data_il7[:,7]
ey_il7_3s = data_il7[:,8]
yy_exp_3s = data_exp[:,7]
ey_exp_3s = data_exp[:,8]
 
yy_chi_4s = data_chi[:,9]
ey_chi_4s = data_chi[:,10]
yy_il7_4s = data_il7[:,9]
ey_il7_4s = data_il7[:,10]
yy_exp_4s = data_exp[:,9]
ey_exp_4s = data_exp[:,10]
 
yy_chi_5s = data_chi[:,11]
ey_chi_5s = data_chi[:,12]
yy_il7_5s = data_il7[:,11]
ey_il7_5s = data_il7[:,12]
yy_exp_5s = data_exp[:,11]
ey_exp_5s = data_exp[:,12]
 
lw=0.7
 
y_chi_gs  = [yy_chi_gs,yy_chi_gs]
yl_chi_gs = [yy_chi_gs-ey_chi_gs,yy_chi_gs-ey_chi_gs]
yu_chi_gs = [yy_chi_gs+ey_chi_gs,yy_chi_gs+ey_chi_gs]
plt.plot(x_chi,yu_chi_gs,color='blue',linewidth=lw)
plt.plot(x_chi,y_chi_gs,color='blue',linewidth=lw)
plt.plot(x_chi,yl_chi_gs,color='blue',linewidth=lw)
 
y_chi_1s  = [yy_chi_1s,yy_chi_1s]
yl_chi_1s = [yy_chi_1s-ey_chi_1s,yy_chi_1s-ey_chi_1s]
yu_chi_1s = [yy_chi_1s+ey_chi_1s,yy_chi_1s+ey_chi_1s]
plt.plot(x_chi,yu_chi_1s,color='blue',linewidth=lw)
plt.plot(x_chi,y_chi_1s,color='blue',linewidth=lw)
plt.plot(x_chi,yl_chi_1s,color='blue',linewidth=lw)
 
y_chi_2s  = [yy_chi_2s,yy_chi_2s]
yl_chi_2s = [yy_chi_2s-ey_chi_2s,yy_chi_2s-ey_chi_2s]
yu_chi_2s = [yy_chi_2s+ey_chi_2s,yy_chi_2s+ey_chi_2s]
plt.plot(x_chi,yu_chi_2s,color='blue',linewidth=lw)
plt.plot(x_chi,y_chi_2s,color='blue',linewidth=lw)
plt.plot(x_chi,yl_chi_2s,color='blue',linewidth=lw)
 
y_chi_3s  = [yy_chi_3s,yy_chi_3s]
yl_chi_3s = [yy_chi_3s-ey_chi_3s,yy_chi_3s-ey_chi_3s]
yu_chi_3s = [yy_chi_3s+ey_chi_3s,yy_chi_3s+ey_chi_3s]
plt.plot(x_chi,yu_chi_3s,color='blue',linewidth=lw)
plt.plot(x_chi,y_chi_3s,color='blue',linewidth=lw)
plt.plot(x_chi,yl_chi_3s,color='blue',linewidth=lw)
 
y_chi_4s  = [yy_chi_4s,yy_chi_4s]
yl_chi_4s = [yy_chi_4s-ey_chi_4s,yy_chi_4s-ey_chi_4s]
yu_chi_4s = [yy_chi_4s+ey_chi_4s,yy_chi_4s+ey_chi_4s]
plt.plot(x_chi,yu_chi_4s,color='blue',linewidth=lw)
plt.plot(x_chi,y_chi_4s,color='blue',linewidth=lw)
plt.plot(x_chi,yl_chi_4s,color='blue',linewidth=lw)
 
y_chi_5s  = [yy_chi_5s,yy_chi_5s]
yl_chi_5s = [yy_chi_5s-ey_chi_5s,yy_chi_5s-ey_chi_5s]
yu_chi_5s = [yy_chi_5s+ey_chi_5s,yy_chi_5s+ey_chi_5s]
plt.plot(x_chi,yu_chi_5s,color='blue',linewidth=lw)
plt.plot(x_chi,y_chi_5s,color='blue',linewidth=lw)
plt.plot(x_chi,yl_chi_5s,color='blue',linewidth=lw)
 
y_il7_gs  = [yy_il7_gs,yy_il7_gs]
yl_il7_gs = [yy_il7_gs-ey_il7_gs,yy_il7_gs-ey_il7_gs]
yu_il7_gs = [yy_il7_gs+ey_il7_gs,yy_il7_gs+ey_il7_gs]
plt.plot(x_il7,yu_il7_gs,color='red',linewidth=lw)
plt.plot(x_il7,y_il7_gs,color='red',linewidth=lw)
plt.plot(x_il7,yl_il7_gs,color='red',linewidth=lw)
 
y_il7_1s  = [yy_il7_1s,yy_il7_1s]
yl_il7_1s = [yy_il7_1s-ey_il7_1s,yy_il7_1s-ey_il7_1s]
yu_il7_1s = [yy_il7_1s+ey_il7_1s,yy_il7_1s+ey_il7_1s]
plt.plot(x_il7,yu_il7_1s,color='red',linewidth=lw)
plt.plot(x_il7,y_il7_1s,color='red',linewidth=lw)
plt.plot(x_il7,yl_il7_1s,color='red',linewidth=lw)
 
y_il7_2s  = [yy_il7_2s,yy_il7_2s]
yl_il7_2s = [yy_il7_2s-ey_il7_2s,yy_il7_2s-ey_il7_2s]
yu_il7_2s = [yy_il7_2s+ey_il7_2s,yy_il7_2s+ey_il7_2s]
plt.plot(x_il7,yu_il7_2s,color='red',linewidth=lw)
plt.plot(x_il7,y_il7_2s,color='red',linewidth=lw)
plt.plot(x_il7,yl_il7_2s,color='red',linewidth=lw)
 
y_il7_3s  = [yy_il7_3s,yy_il7_3s]
yl_il7_3s = [yy_il7_3s-ey_il7_3s,yy_il7_3s-ey_il7_3s]
yu_il7_3s = [yy_il7_3s+ey_il7_3s,yy_il7_3s+ey_il7_3s]
plt.plot(x_il7,yu_il7_3s,color='red',linewidth=lw)
plt.plot(x_il7,y_il7_3s,color='red',linewidth=lw)
plt.plot(x_il7,yl_il7_3s,color='red',linewidth=lw)
 
y_il7_4s  = [yy_il7_4s,yy_il7_4s]
yl_il7_4s = [yy_il7_4s-ey_il7_4s,yy_il7_4s-ey_il7_4s]
yu_il7_4s = [yy_il7_4s+ey_il7_4s,yy_il7_4s+ey_il7_4s]
plt.plot(x_il7,yu_il7_4s,color='red',linewidth=lw)
plt.plot(x_il7,y_il7_4s,color='red',linewidth=lw)
plt.plot(x_il7,yl_il7_4s,color='red',linewidth=lw)
 
y_il7_5s  = [yy_il7_5s,yy_il7_5s]
yl_il7_5s = [yy_il7_5s-ey_il7_5s,yy_il7_5s-ey_il7_5s]
yu_il7_5s = [yy_il7_5s+ey_il7_5s,yy_il7_5s+ey_il7_5s]
plt.plot(x_il7,yu_il7_5s,color='red',linewidth=lw)
plt.plot(x_il7,y_il7_5s,color='red',linewidth=lw)
plt.plot(x_il7,yl_il7_5s,color='red',linewidth=lw)
 
y_exp_gs  = [yy_exp_gs,yy_exp_gs]
yl_exp_gs = [yy_exp_gs-ey_exp_gs,yy_exp_gs-ey_exp_gs]
yu_exp_gs = [yy_exp_gs+ey_exp_gs,yy_exp_gs+ey_exp_gs]
plt.plot(x_exp,yu_exp_gs,color='green',linewidth=lw)
plt.plot(x_exp,y_exp_gs,color='green',linewidth=lw)
plt.plot(x_exp,yl_exp_gs,color='green',linewidth=lw)
 
y_exp_1s  = [yy_exp_1s,yy_exp_1s]
yl_exp_1s = [yy_exp_1s-ey_exp_1s,yy_exp_1s-ey_exp_1s]
yu_exp_1s = [yy_exp_1s+ey_exp_1s,yy_exp_1s+ey_exp_1s]
plt.plot(x_exp,yu_exp_1s,color='green',linewidth=lw)
plt.plot(x_exp,y_exp_1s,color='green',linewidth=lw)
plt.plot(x_exp,yl_exp_1s,color='green',linewidth=lw)
 
y_exp_2s  = [yy_exp_2s,yy_exp_2s]
yl_exp_2s = [yy_exp_2s-ey_exp_2s,yy_exp_2s-ey_exp_2s]
yu_exp_2s = [yy_exp_2s+ey_exp_2s,yy_exp_2s+ey_exp_2s]
plt.plot(x_exp,yu_exp_2s,color='green',linewidth=lw)
plt.plot(x_exp,y_exp_2s,color='green',linewidth=lw)
plt.plot(x_exp,yl_exp_2s,color='green',linewidth=lw)
 
y_exp_3s  = [yy_exp_3s,yy_exp_3s]
yl_exp_3s = [yy_exp_3s-ey_exp_3s,yy_exp_3s-ey_exp_3s]
yu_exp_3s = [yy_exp_3s+ey_exp_3s,yy_exp_3s+ey_exp_3s]
plt.plot(x_exp,yu_exp_3s,color='green',linewidth=lw)
plt.plot(x_exp,y_exp_3s,color='green',linewidth=lw)
plt.plot(x_exp,yl_exp_3s,color='green',linewidth=lw)
 
y_exp_4s  = [yy_exp_4s,yy_exp_4s]
yl_exp_4s = [yy_exp_4s-ey_exp_4s,yy_exp_4s-ey_exp_4s]
yu_exp_4s = [yy_exp_4s+ey_exp_4s,yy_exp_4s+ey_exp_4s]
plt.plot(x_exp,yu_exp_4s,color='green',linewidth=lw)
plt.plot(x_exp,y_exp_4s,color='green',linewidth=lw)
plt.plot(x_exp,yl_exp_4s,color='green',linewidth=lw)
 
y_exp_5s  = [yy_exp_5s,yy_exp_5s]
yl_exp_5s = [yy_exp_5s-ey_exp_5s,yy_exp_5s-ey_exp_5s]
yu_exp_5s = [yy_exp_5s+ey_exp_5s,yy_exp_5s+ey_exp_5s]
plt.plot(x_exp,yu_exp_5s,color='green',linewidth=lw)
plt.plot(x_exp,y_exp_5s,color='green',linewidth=lw)
plt.plot(x_exp,yl_exp_5s,color='green',linewidth=lw)
 
 
#plt.fill_between(x_exp, yl_exp_5s, yu_exp_5s, facecolor='yellow', alpha=0.5)
plt.fill_between(x_exp, yl_exp_5s, yu_exp_5s, alpha=0.5, facecolor='b',edgecolor = 'none')
#plt.show()
plt.axis([0, 40, -100, -0.2])
#plt.xticks([])
ml = MultipleLocator(4)
plt.axes().yaxis.set_minor_locator(ml)
ftz=12
plt.text(4.0, -18,'$^3$H', fontsize=ftz)
plt.text(7.0, -18,'$^3$He', fontsize=ftz)
plt.text(2.5, -38,'$^4$He', fontsize=ftz)
plt.text(6.0, -38,'$^6$He', fontsize=ftz)
plt.text(9.8, -42,'$^6$Li', fontsize=ftz)
plt.text(12.8, -48,'$^7$Li', fontsize=ftz)
plt.text(15.5,-40,'$^8$He', fontsize=ftz)
plt.text(19.0,-49,'$^8$Li', fontsize=ftz)
plt.text(22.0,-65,'$^8$Be', fontsize=ftz)
plt.text(25.0,-55,'$^9$Li', fontsize=ftz)
plt.text(27.0,-68,'$^9$Be', fontsize=ftz)
plt.text(30.0,-75,'$^{10}$Be', fontsize=ftz)
plt.text(34.0,-75,'$^{10}$B', fontsize=ftz)
savefig('spectrum.pdf')
Reply
#2
Lions, Tigers and Bears. Oh my!

This is some of the most awful code to look at!

Quote:maybe using do loops
they are called for loops in python.

Do you have the specs?
It might be easier to rewrite.

Also, when posting error, please include the full traceback as there is
important information to help with debugging, including line number of the
code causing the error.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020