I want to make gif which the color of one part of the plot is changing. The plot includes two lines and I am filling the area between the curves by colors. This is the plot:
This code makes this plot:
import matplotlib.pyplot as plt
upperline = [[0, 5], [3, 3]]
lowerline = [[0, 5], [2, 1]]
fig, axs = plt.subplots(1,1, figsize=(15,4))
axs.plot(upperline [0], upperline [1], color='k', ls='--')
axs.plot(lowerline [0], lowerline [1], color='k', ls='--')
axs.set_xlim([0,5])
axs.set_ylim([0,4])
axs.set_xlabel('X', fontsize=10, family='Arial')
axs.set_ylabel('Z', fontsize=10, family='Arial')
plt.fill_between(upperline [0], [4, 4], upperline[1], color='silver')
plt.fill_between(upperline [0], upperline[1], lowerline[1], color='grey')
plt.fill_between(upperline [0], lowerline[1], [0, 0], color='oldlace')
Then, I want to have a gif of the plot in which the color between lines is changing. For example firstly it is grey, in next frame change to blue, and so on (based on the list named colors).
I tried the following code block but it did not give me what I want:
colours = ['b', 'c', 'yellow', 'orange', '#EE4B2B']
upperline = [[0, 5], [3, 3]]
lowerline = [[0, 5], [2, 1]]
fig, axs = plt.subplots(1,1, figsize=(15,4))
def animation_function(i):
axs.plot(upperline [0], upperline [1], color='k', ls='--')
axs.plot(lowerline [0], lowerline [1], color='k', ls='--')
axs.set_xlim([0,5])
axs.set_ylim([0,4])
axs.set_xlabel('X', fontsize=10, family='Arial')
axs.set_ylabel('Z', fontsize=10, family='Arial')
plt.fill_between(upperline [0], [4, 4], upperline[1], color='silver')
plt.fill_between(upperline [0], upperline[1], lowerline[1], color=i)
plt.fill_between(upperline [0], lowerline[1], [0, 0], color='oldlace')
return axs
animation = FuncAnimation(fig,
func = animation_function,
frames = np.arange(0, 10, 0.1),
interval = 10)
fn = 'color_change'
ani.save(fn+'.gif', writer='imagemagick',fps=25,dpi=50)
