0

I try to adapt this code animated plot line to similar situation but using hist in lieu of line plot and expend it to 4 subplot. It seem to be an easy task with this great exemple but I struggle an error on the adaptation.

line1, = ax1.hist([], HIST_BINS1, lw=1, density=True, color='orange', alpha=0.5, 
label='1 an')
ValueError: too many values to unpack (expected 1)

I try to remove the "comma" but don't seem to be the solution, thank to all one who will take time to check my code

import pandas as pd
import numpy as np
from typing import Generator
import matplotlib.pyplot as plt
import matplotlib.animation as animation

path = "E:/data/HQ/meteo_app_moy/"
file = "Rouge (040204)_1062.csv"
data = pd.read_csv(path + file, usecols=[0, 1, 2, 3, 4], sep=";", header=None,         
                  decimal=",", thousands=".")
data['date'] = pd.date_range(start='1/1/1950', end='31/12/2021')
data = data.set_index('date')
yrs = list(data.index.year.unique())
HIST_BINS1 = np.linspace(data[3].min(), data[3].max(), round(data[3].max() / 2))
HIST_BINS2 = np.linspace(data[4].min(), data[4].max(), round(data[4].max() / 2))
HIST_BINS3 = np.linspace(-40, 40, 40)
HIST_BINS4 = np.linspace(0, data[0].max(), 100)


def data_gen():
    cnt = 0
    while cnt <= len(yrs):
        cnt += 1
        y1 = data.iloc[data.index.year.isin([yrs[cnt]]), 3]  # pluie
        y1 = y1[y1 > 0]
        y2 = data.iloc[data.index.year.isin([yrs[cnt]]), 4]  # neige
        y2 = y2[y2 > 1]
        y3 = data[[1, 2]].mean(axis=1)  # Tmoy
        y3 = y3.iloc[y3.index.year.isin([yrs[cnt]])]
        y4 = data.iloc[data.index.year.isin([yrs[cnt]]), 0]  # App
        yield y1, y2, y3, y4


# create a figure with two subplots
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

# add a fixed hist as ref
ax1.hist(data.loc[data[3]>1,3], HIST_BINS1, lw=1, density=True, color='steelblue',         
           alpha=0.5, label='histo')
# intialize hist objects (one in each axes)
line1, = ax1.hist([], HIST_BINS1, lw=1, density=True, color='orange', alpha=0.5,         
                  label='1 an')
line2, = ax2.hist([], HIST_BINS2, lw=1, density=True)
line3, = ax3.hist([], HIST_BINS3, lw=1, density=True)
line4, = ax4.hist([], HIST_BINS4, lw=1, density=True)
line = [line1, line2, line3, line4]

# initialize the data arrays
y1data, y2data, y3data, y4data = [], [], [], []


def run(data):
    # update the data
    y1, y2, y3, y4 = data
    # xdata.append(t)
    y1data.append(y1)
    y2data.append(y2)
    y3data.append(y3)
    y4data.append(y4)

    # update the data of both line objects
    line[0].set_data(y1data)
    line[1].set_data(y2data)
    line[2].set_data(y3data)
    line[3].set_data(y4data)
    return line


ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=2,
                          repeat=False)
plt.show()

PS: sorry for my english Im working hard on it

Jonathan Roy
  • 405
  • 1
  • 6
  • 18
  • Did you look into [this animated histogram from the tutorial](https://matplotlib.org/stable/gallery/animation/animated_histogram.html)? And into [Defining multiple plots to be animated with a for loop](https://stackoverflow.com/questions/21937976/defining-multiple-plots-to-be-animated-with-a-for-loop-in-matplotlib)? – JohanC Feb 12 '22 at 21:33
  • yes but when I try to merge all go wrong – Jonathan Roy Feb 21 '22 at 21:56

0 Answers0