首页 > 解决方案 > 如何清除现金 go.Figure() 情节?

问题描述

我正在使用一个通用类,它可以绘制多个条形图堆叠或与 Plotly 分组。我的问题是我的函数也在我未来的图中使用旧数据,即使是我的__init__().

这是我的代码:

class multiple_bar_chart:
    '''
    This class allow the user to create a multiple bar chart (stack or group)
    and add as many features as he wants.

    /!\ X column must be in the same format for each new data

    :param barmode : type of bar chart

    => add_bar : add a new bar to your plot
    => plot_plt : display your chart
    '''

    def __init__(self, figure=go.Figure(), barmode='stack'):
        self.fig = figure
        self.type_bar_chart = barmode  # group or stacked

    def add_bar(self, input_df, x_column, y_column, data_legend, color=None):
        self.fig.add_bar(name=data_legend,
                         x=input_df[x_column],
                         y=input_df[y_column],
                         marker_color=color)

    def plot_plt(self):
        self.fig.update_layout(barmode=self.type_bar_chart)
        st.markdown(download_plot(self.fig), unsafe_allow_html=True)
        st.plotly_chart(self.fig, use_container_width=True)

plot1 = multiple_bar_chart(barmode = 'stack')
plot2 = multiple_bar_chart(barmode = 'group') # This class is also using the plot1 data

我知道我的问题来自go.Figure(),但我不知道为什么我__init__()没有初始化它。

有人有解决这个问题的想法吗?我将不胜感激!

另外,如果您对提高我的编程技能有一些建议,我正在倾听!:)

感谢,并有一个愉快的一天 :)

标签: pythonoopplotlyplotly-python

解决方案


推荐阅读