在迭代中使用 plotly 显示多个图

问题描述 投票:0回答:0

想用pandas的ploty技术执行for循环显示多张图,但是显示的时候有问题。这仅显示第一条曲线,其他曲线为空。所以我不知道该怎么做,因为我是新手。 这是实现所有情节的代码。

import pandas as pd
from typing import Any
from datetime import datetime, date, timedelta
import matplotlib.pyplot as plt
import numpy as np
import plotly.express as px
import random

df = pd.read_csv('data/compute30m_cumulate.csv')

def preprocessing(sdf: Any)-> pd.DataFrame:
    """
    description: data preprocessing for display stacked bar diagram

    Args:
        sdf (DataFrame): a spark dataframe
    Returns:
        dfp (DataFrame): pandas DataFrame preprocessing
    """        
    dfp = sdf .toPandas()
    dfp = dfp.rename(columns={"deviceId": "building"})
    dfp.loc[:, 'time'] = pd.to_datetime(dfp.loc[:, 'time'])
    dfp.time = dfp.time.dt.date
    return dfp

def get_cumulate_data_by_measure_period(dfp: pd.DataFrame, measure: str, period: str)->pd.DataFrame:
    """
    description: get cumulate data by measure and period

    Args:
        dfp (DataFrame): a pandas dataframe
        measure (str): a measure we want to obtain data
        period (str): a period we want to obtain data
    Returns:
        dfp (DataFrame): pandas DataFrame preprocessing
    """    
    dfp = dfp[dfp.measure == measure]
    if period == '1Y':
        return dfp[dfp.cumul_measure.str.contains('YEARLY')]
    elif period == '1M':
        the_date = date(date.today().year, date.today().month-2, date.today().day)
        return dfp[(dfp["cumul_measure"].str.contains('MONTHLY')) & (dfp.time > the_date)]
    elif period == '1D':
        the_date = date.today() - timedelta(30)
        return dfp[(dfp["cumul_measure"].str.contains('DAILY')) & (dfp.time > the_date)]        

def plot_timeseries_bar(dfp: pd.DataFrame, _x: str, _y: str, _color: str, _title: str,  _text_auto: bool = True):
    """
    description: plot barplot timeseries

    Args:
        dfp (pd.DataFrame): a pandas DataFrame by measure and period
        _x (str): a col name that represent x axis
        _y (str) : a col name that represent y axis
        _color (str): a col name for color ex: color="building"
        _title (str): a title of diagram
        _text_auto (bool): indication of value in the bar diagram 
    Returns:
        None
    """    
    n = random.randint(50, 100)
    fig_n = px.bar(dfp, x=_x, y=_y, color=_color, title=_title, text_auto=_text_auto)
    fig_n.show()

def _titles(period: str) -> str:
    """format title by period"""
    if period == "1D":
        p = "daily"
    elif period == "1M":
        p = "monthly"
    elif period == "1Y":
        p = "yearly"
    return p

def _titles(period: str) -> str:
    """format title by period"""
    if period == "1D":
        p = "daily"
    elif period == "1M":
        p = "monthly"
    elif period == "1Y":
        p = "yearly"
    return p



def generate_diagrams(sdf: Any) -> None:
    periods = ["1D", "1M", "1Y"]
    measures = [
        "CO2_PRODUCED",
        "CO2_PRODUCED_LEGACY",
        "CO2_PRODUCED_REDUCTION",
        "ENERGY_CONSUMED",
        "ENERGY_CONSUMED_LEGACY",
        "ENERGY_CONSUMED_REDUCTION",
    ]    
    dfp = preprocessing(sdf)
    for measure in measures:
        for period in periods:
            dfp = get_cumulate_data_by_measure_period(dfp, measure, period)
            plot_timeseries_bar(dfp, "time", _y="cumul_by_period", _color="building", _title=f"{measure} cumulated by {_titles(period)}",  _text_auto = True)

但这只是 cumul co2 的图的结果。我不明白为什么其他情节没有出现。

python pandas dataframe plotly
© www.soinside.com 2019 - 2024. All rights reserved.