Matplotlib Version - 3.5.2
I am using FuncAnimation for dynamic plotting. I also want to change the facecolor of particular regions of graphs as per the points. The color is getting changed, but only gets reflected when I resize the window.
Solution for the problem?
Attaching the code :-
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.set_xlim(-10,10)
ax1.set_ylim(-10,10)
ax1.spines['left'].set_position('zero')
ax1.spines['bottom'].set_position('zero')
ax1.xaxis.label.set_color('white')
ax1.yaxis.label.set_color('white')
ax1.tick_params(axis='x', colors='white')
ax1.tick_params(axis='y', colors='white')
# ax1.axvspan(0, 10, ymin=0.5, facecolor='red', alpha=0.5)
# ax1.axvspan(-10, 0, ymin=0.5, facecolor='green', alpha=0.5)
# ax1.axvspan(-10, 0, ymax=0.5, facecolor='black', alpha=0.5)
# ax1.axvspan(0, 10, ymax=0.5, facecolor='blue', alpha=0.5)
# ax1.set_facecolor('midnightblue')
t, = ax1.plot([], [], c='red', marker="o", ls="", alpha=1, label="Range Profile")
# a = 0
# b = 0
# c = 0
# d = 0
def readAndParseData():
global detObj
x = np.random.randint(-10,10,15)
y = np.random.randint(-10,10,15)
detObj = {"x": x, "z": y}
def func_init():
# global a,b,c,d
t.set_data([],[])
# a = ax1.axvspan(-10, 0, ymin=0.5, facecolor='midnightblue')
# b = ax1.axvspan(0, 10, ymax=0.5, facecolor='midnightblue')
# c = ax1.axvspan(0, 10, ymin=0.5, facecolor='midnightblue')
# d = ax1.axvspan(-10, 0, ymax=0.5, facecolor='midnightblue')
return t,
def update(i):
readAndParseData()
z = detObj["z"]
# print(z)
# print("\n")
x = detObj["x"]
# y = detObj["y"]
# print(x)
# print(x)
a = ax1.axvspan(-10, 0, ymin=0.5, facecolor='midnightblue')
b = ax1.axvspan(0, 10, ymax=0.5, facecolor='midnightblue')
c = ax1.axvspan(0, 10, ymin=0.5, facecolor='midnightblue')
d = ax1.axvspan(-10, 0, ymax=0.5, facecolor='midnightblue')
# ax1.axvspan(-10, 10, facecolor='midnightblue', alpha=1)
for i in range(len(x)):
if x[i]<0 and z[i]>=0:
# a = ax1.axvspan(-10, 0, ymin=0.5, facecolor='lime', alpha=0.5)
a.set_facecolor('lime')
print(3)
elif x[i]>=0 and z[i]<0:
# b = ax1.axvspan(0, 10, ymax=0.5, facecolor='lime', alpha=0.5)
b.set_facecolor('lime')
print(1)
elif x[i]>=0 and z[i]>=0:
# c = ax1.axvspan(0, 10, ymin=0.5, facecolor='lime', alpha=0.5)
c.set_facecolor('lime')
print(2)
else:
# d = ax1.axvspan(-10, 0, ymax=0.5, facecolor='lime', alpha=0.5)
d.set_facecolor('lime')
print(4)
t.set_data(x,z)
return t,
ani = FuncAnimation(fig, update, init_func = func_init, interval=110, blit=True, save_count=0, cache_frame_data=False)
plt.show()
PS: In actuality, the readAndParseData() returns something else not random integers. I am making it return random integers for the sake of posting here…