情节覆盖没有痕迹的传奇名称

发布于 2025-02-09 06:40:13 字数 1751 浏览 2 评论 0原文

我正在尝试更改传奇名称,但我找到了许多答案,但是如果您不使用痕迹,则它们都没有起作用。我学会了更改传奇标题,但没有更改名称。 这是我的代码,希望有人可以提供帮助:

import pandas as pd
import plotly.express as px
import numpy as np

def camasRegion():  
    df = pd.read_csv('https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto52/Camas_UCI.csv')
    dates = df.columns #for some reason I can't use this as x axis
    habilitadas = []
    ocupadasCovid = []
    ocupadasNoCovid = []
    region = 'Ñuble'

    for i in range(len(df)):
        if df.values[i][0] == region:
            if df.values[i][1] == 'Camas UCI habilitadas':          
                for x in range(len(df.columns)):
                    habilitadas.append(df.values[i][x])

            if df.values[i][1] == 'Camas UCI ocupadas COVID-19':            
                for x in range(len(df.columns)):
                    ocupadasCovid.append(df.values[i][x])

            if df.values[i][1] == 'Camas UCI ocupadas no COVID-19':         
                for x in range(len(df.columns)):
                    ocupadasNoCovid.append(df.values[i][x])     

    fig = px.line(df, 
        y = [habilitadas, ocupadasCovid, ocupadasNoCovid],
        x = range(len(fechas)), #I'm doing this because x = dates doesn't work
        title='Camas por región: ('+region+')',)
        

    fig.add_annotation(
        x = 1, y = -0.1, 
        text = 'Fuente: Datos obtenidos desde el Ministerio de Ciencia.', 
        showarrow = False, xref='paper', yref='paper', 
        xanchor='right', yanchor='auto', xshift=0, yshift=-20
        )
    fig.update_layout(
        legend_title_text='Estado de cama',
        xaxis_title="Días",
        yaxis_title="Cantidad de camas",
        )
    fig.show()


camasRegion()

I'm trying to change the legend names and I've found many answers but none of them works if you are not using traces. I learned to change the legend title but not the names.
Here it is my code, hopefully somebody can help:

import pandas as pd
import plotly.express as px
import numpy as np

def camasRegion():  
    df = pd.read_csv('https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto52/Camas_UCI.csv')
    dates = df.columns #for some reason I can't use this as x axis
    habilitadas = []
    ocupadasCovid = []
    ocupadasNoCovid = []
    region = 'Ñuble'

    for i in range(len(df)):
        if df.values[i][0] == region:
            if df.values[i][1] == 'Camas UCI habilitadas':          
                for x in range(len(df.columns)):
                    habilitadas.append(df.values[i][x])

            if df.values[i][1] == 'Camas UCI ocupadas COVID-19':            
                for x in range(len(df.columns)):
                    ocupadasCovid.append(df.values[i][x])

            if df.values[i][1] == 'Camas UCI ocupadas no COVID-19':         
                for x in range(len(df.columns)):
                    ocupadasNoCovid.append(df.values[i][x])     

    fig = px.line(df, 
        y = [habilitadas, ocupadasCovid, ocupadasNoCovid],
        x = range(len(fechas)), #I'm doing this because x = dates doesn't work
        title='Camas por región: ('+region+')',)
        

    fig.add_annotation(
        x = 1, y = -0.1, 
        text = 'Fuente: Datos obtenidos desde el Ministerio de Ciencia.', 
        showarrow = False, xref='paper', yref='paper', 
        xanchor='right', yanchor='auto', xshift=0, yshift=-20
        )
    fig.update_layout(
        legend_title_text='Estado de cama',
        xaxis_title="Días",
        yaxis_title="Cantidad de camas",
        )
    fig.show()


camasRegion()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2025-02-16 06:40:13

您不能使用日期,因为除了日期外,df.columns包含regionserie。我简化了您的代码,这也使其更具可读性。尝试一下,让我知道,如果它仍然对您不起作用。

import pandas as pd
import plotly.graph_objects as go
import numpy as np

def camasRegion():  
    df = pd.read_csv('https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto52/Camas_UCI.csv')
    region = "Ñuble"
    target_region = df.query('Region == @region').drop('Region', axis=1).set_index('Serie').T
    
    chart = []
    for i in target_region:
        if i != 'Camas base (2019)':  #or you can easily drop it from your dataset
            chart += [go.Scatter(x=target_region.index,y=target_region[i], name=i, mode='lines')]
            
    fig = go.Figure(chart)
    fig.update_layout(title={'text':f'Camas por región: ({region})', 'x':.45},
                      template='plotly_white', hovermode='x',
                      legend_title_text='Estado de cama',
                      xaxis_title="Días",
                      yaxis_title="Cantidad de camas")

    fig.add_annotation(
        x = 1, y = -0.1, 
        text = 'Fuente: Datos obtenidos desde el Ministerio de Ciencia.', 
        showarrow = False, xref='paper', yref='paper', 
        xanchor='right', yanchor='auto', xshift=0, yshift=-20
        )
    fig.show()


camasRegion()

结果:

You can't use dates, because df.columns besides dates contains strings Region and Serie. I simplified your code, it also made it more readable. Try this and let me know, if it still not working for you.

import pandas as pd
import plotly.graph_objects as go
import numpy as np

def camasRegion():  
    df = pd.read_csv('https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto52/Camas_UCI.csv')
    region = "Ñuble"
    target_region = df.query('Region == @region').drop('Region', axis=1).set_index('Serie').T
    
    chart = []
    for i in target_region:
        if i != 'Camas base (2019)':  #or you can easily drop it from your dataset
            chart += [go.Scatter(x=target_region.index,y=target_region[i], name=i, mode='lines')]
            
    fig = go.Figure(chart)
    fig.update_layout(title={'text':f'Camas por región: ({region})', 'x':.45},
                      template='plotly_white', hovermode='x',
                      legend_title_text='Estado de cama',
                      xaxis_title="Días",
                      yaxis_title="Cantidad de camas")

    fig.add_annotation(
        x = 1, y = -0.1, 
        text = 'Fuente: Datos obtenidos desde el Ministerio de Ciencia.', 
        showarrow = False, xref='paper', yref='paper', 
        xanchor='right', yanchor='auto', xshift=0, yshift=-20
        )
    fig.show()


camasRegion()

The result:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文