1. <i id='8v2YO'><tr id='8v2YO'><dt id='8v2YO'><q id='8v2YO'><span id='8v2YO'><b id='8v2YO'><form id='8v2YO'><ins id='8v2YO'></ins><ul id='8v2YO'></ul><sub id='8v2YO'></sub></form><legend id='8v2YO'></legend><bdo id='8v2YO'><pre id='8v2YO'><center id='8v2YO'></center></pre></bdo></b><th id='8v2YO'></th></span></q></dt></tr></i><div id='8v2YO'><tfoot id='8v2YO'></tfoot><dl id='8v2YO'><fieldset id='8v2YO'></fieldset></dl></div>
      <bdo id='8v2YO'></bdo><ul id='8v2YO'></ul>

      <legend id='8v2YO'><style id='8v2YO'><dir id='8v2YO'><q id='8v2YO'></q></dir></style></legend><tfoot id='8v2YO'></tfoot>

      <small id='8v2YO'></small><noframes id='8v2YO'>

    1. Ploly:如何在条形图中添加趋势线?

      时间:2024-08-22
        <bdo id='bZVtr'></bdo><ul id='bZVtr'></ul>
          <tbody id='bZVtr'></tbody>
          <tfoot id='bZVtr'></tfoot>
            <legend id='bZVtr'><style id='bZVtr'><dir id='bZVtr'><q id='bZVtr'></q></dir></style></legend>

            <small id='bZVtr'></small><noframes id='bZVtr'>

            <i id='bZVtr'><tr id='bZVtr'><dt id='bZVtr'><q id='bZVtr'><span id='bZVtr'><b id='bZVtr'><form id='bZVtr'><ins id='bZVtr'></ins><ul id='bZVtr'></ul><sub id='bZVtr'></sub></form><legend id='bZVtr'></legend><bdo id='bZVtr'><pre id='bZVtr'><center id='bZVtr'></center></pre></bdo></b><th id='bZVtr'></th></span></q></dt></tr></i><div id='bZVtr'><tfoot id='bZVtr'></tfoot><dl id='bZVtr'><fieldset id='bZVtr'></fieldset></dl></div>
              • 本文介绍了Ploly:如何在条形图中添加趋势线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试将趋势线添加到plotly绘制的条形图

                编码

                import plotly.express as px
                
                fig = px.bar(count, x="date", y="count",trendline="ols")
                
                fig.update_layout(
                    xaxis_title="Date",
                    yaxis_title = "Count"
                )
                
                fig.show()
                

                错误:

                ---------------------------------------------------------------------------
                TypeError                                 Traceback (most recent call last)
                <ipython-input-129-8b01de219d3c> in <module>
                ----> 1 fig = px.bar(count, x="date", y="count",trendline="ols")
                      2 
                      3 fig.update_layout(
                      4     xaxis_title="Date",
                      5     yaxis_title = "Count"
                
                TypeError: bar() got an unexpected keyword argument 'trendline'
                

                这里是 data

                如何将趋势线成功添加到此绘图?

                推荐答案

                px.bar没有trendline方法。由于您正在尝试trendline="ols",我猜您希望创建一条线性趋势线。看看您的数据,线性趋势线可能不是对您的数据集的最佳描述:

                所以您必须自己添加趋势线。您仍然可以使用go.Bar使用条形图,但可能会考虑将趋势线显示为线,而不是更多的条形图。

                关于非线性趋势,仔细研究scikit或statsmodels应该是非常值得的。一种简单的方法是在重新编码数据集之后估计对数线性趋势。您将看到,这比简单的线性趋势更能"捕捉"variable的指数增长:

                但这是否足够好呢?我会让你来决定的。正如我已经说过的,您应该仔细查看链接的资源。

                1地块代码:

                from sklearn.linear_model import LinearRegression
                import plotly.graph_objects as go
                import pandas as pd
                import numpy as np
                import datetime
                
                # data
                df=pd.DataFrame({'date': {0: '12.10.2019',
                                  1: '13.10.2019',
                                  2: '14.10.2019',
                                  3: '15.10.2019',
                                  4: '16.10.2019',
                                  5: '17.10.2019',
                                  6: '18.10.2019',
                                  7: '19.10.2019',
                                  8: '20.10.2019',
                                  9: '21.10.2019',
                                  10: '22.10.2019',
                                  11: '23.10.2019',
                                  12: '24.10.2019',
                                  13: '25.10.2019',
                                  14: '26.10.2019',
                                  15: '27.10.2019',
                                  16: '28.10.2019',
                                  17: '29.10.2019',
                                  18: '30.10.2019',
                                  19: '31.10.2019',
                                  20: '01.11.2019',
                                  21: '02.11.2019',
                                  22: '03.11.2019',
                                  23: '04.11.2019',
                                  24: '05.11.2019',
                                  25: '06.11.2019',
                                  26: '07.11.2019',
                                  27: '08.11.2019',
                                  28: '09.11.2019',
                                  29: '10.11.2019',
                                  30: '11.11.2019',
                                  31: '12.11.2019',
                                  32: '13.11.2019',
                                  33: '14.11.2019',
                                  34: '15.11.2019',
                                  35: '16.11.2019',
                                  36: '17.11.2019',
                                  37: '18.11.2019',
                                  38: '19.11.2019',
                                  39: '20.11.2019',
                                  40: '21.11.2019',
                                  41: '22.11.2019',
                                  42: '23.11.2019',
                                  43: '24.11.2019',
                                  44: '25.11.2019',
                                  45: '26.11.2019',
                                  46: '27.11.2019',
                                  47: '28.11.2019',
                                  48: '29.11.2019',
                                  49: '30.11.2019',
                                  50: '01.12.2019',
                                  51: '02.12.2019',
                                  52: '03.12.2019',
                                  53: '04.12.2019',
                                  54: '05.12.2019',
                                  55: '06.12.2019',
                                  56: '07.12.2019',
                                  57: '08.12.2019',
                                  58: '09.12.2019',
                                  59: '10.12.2019',
                                  60: '11.12.2019',
                                  61: '12.12.2019',
                                  62: '13.12.2019',
                                  63: '14.12.2019',
                                  64: '15.12.2019',
                                  65: '16.12.2019',
                                  66: '17.12.2019',
                                  67: '18.12.2019',
                                  68: '19.12.2019',
                                  69: '20.12.2019',
                                  70: '21.12.2019',
                                  71: '22.12.2019',
                                  72: '23.12.2019',
                                  73: '24.12.2019',
                                  74: '25.12.2019',
                                  75: '26.12.2019',
                                  76: '27.12.2019',
                                  77: '28.12.2019',
                                  78: '29.12.2019',
                                  79: '30.12.2019',
                                  80: '31.12.2019',
                                  81: '01.01.2020',
                                  82: '02.01.2020',
                                  83: '03.01.2020',
                                  84: '04.01.2020',
                                  85: '05.01.2020',
                                  86: '06.01.2020',
                                  87: '07.01.2020',
                                  88: '08.01.2020',
                                  89: '09.01.2020',
                                  90: '10.01.2020',
                                  91: '11.01.2020',
                                  92: '12.01.2020',
                                  93: '13.01.2020',
                                  94: '14.01.2020',
                                  95: '15.01.2020',
                                  96: '16.01.2020',
                                  97: '17.01.2020',
                                  98: '18.01.2020',
                                  99: '19.01.2020',
                                  100: '20.01.2020',
                                  101: '21.01.2020',
                                  102: '22.01.2020',
                                  103: '23.01.2020',
                                  104: '24.01.2020',
                                  105: '25.01.2020',
                                  106: '26.01.2020',
                                  107: '27.01.2020',
                                  108: '28.01.2020',
                                  109: '29.01.2020',
                                  110: '30.01.2020',
                                  111: '31.01.2020'},
                                 'count': {0: 19,
                                  1: 12,
                                  2: 13,
                                  3: 18,
                                  4: 13,
                                  5: 19,
                                  6: 15,
                                  7: 14,
                                  8: 12,
                                  9: 6,
                                  10: 15,
                                  11: 15,
                                  12: 12,
                                  13: 17,
                                  14: 13,
                                  15: 14,
                                  16: 11,
                                  17: 11,
                                  18: 11,
                                  19: 9,
                                  20: 14,
                                  21: 15,
                                  22: 11,
                                  23: 13,
                                  24: 14,
                                  25: 14,
                                  26: 16,
                                  27: 16,
                                  28: 17,
                                  29: 13,
                                  30: 14,
                                  31: 14,
                                  32: 12,
                                  33: 6,
                                  34: 14,
                                  35: 12,
                                  36: 16,
                                  37: 15,
                                  38: 19,
                                  39: 18,
                                  40: 17,
                                  41: 17,
                                  42: 17,
                                  43: 17,
                                  44: 19,
                                  45: 15,
                                  46: 20,
                                  47: 21,
                                  48: 19,
                                  49: 18,
                                  50: 22,
                                  51: 21,
                                  52: 21,
                                  53: 18,
                                  54: 21,
                                  55: 23,
                                  56: 22,
                                  57: 17,
                                  58: 25,
                                  59: 28,
                                  60: 24,
                                  61: 26,
                                  62: 23,
                                  63: 23,
                                  64: 22,
                                  65: 26,
                                  66: 25,
                                  67: 24,
                                  68: 24,
                                  69: 24,
                                  70: 24,
                                  71: 27,
                                  72: 26,
                                  73: 28,
                                  74: 28,
                                  75: 29,
                                  76: 34,
                                  77: 31,
                                  78: 38,
                                  79: 37,
                                  80: 34,
                                  81: 45,
                                  82: 43,
                                  83: 44,
                                  84: 49,
                                  85: 47,
                                  86: 54,
                                  87: 49,
                                  88: 57,
                                  89: 62,
                                  90: 65,
                                  91: 55,
                                  92: 67,
                                  93: 69,
                                  94: 72,
                                  95: 45,
                                  96: 89,
                                  97: 87,
                                  98: 90,
                                  99: 121,
                                  100: 140,
                                  101: 173,
                                  102: 163,
                                  103: 171,
                                  104: 183,
                                  105: 165,
                                  106: 189,
                                  107: 201,
                                  108: 230,
                                  109: 290,
                                  110: 311,
                                  111: 321}})
                Y=df['count']
                X=df.index
                
                # regression
                reg = LinearRegression().fit(np.vstack(X), Y)
                df['bestfit'] = reg.predict(np.vstack(X))
                
                # plotly figure setup
                fig=go.Figure()
                fig.add_trace(go.Bar(name='X vs Y', x=X, y=Y.values))
                fig.add_trace(go.Scatter(name='line of best fit', x=X, y=df['bestfit'], mode='lines'))
                
                # plotly figure layout
                fig.update_layout(xaxis_title = 'X', yaxis_title = 'Y')
                
                fig.show()
                

                绘图2代码:

                from sklearn.linear_model import LinearRegression
                import plotly.graph_objects as go
                import pandas as pd
                import numpy as np
                import datetime
                
                # data
                df=pd.DataFrame({'date': {0: '12.10.2019',
                                  1: '13.10.2019',
                                  2: '14.10.2019',
                                  3: '15.10.2019',
                                  4: '16.10.2019',
                                  5: '17.10.2019',
                                  6: '18.10.2019',
                                  7: '19.10.2019',
                                  8: '20.10.2019',
                                  9: '21.10.2019',
                                  10: '22.10.2019',
                                  11: '23.10.2019',
                                  12: '24.10.2019',
                                  13: '25.10.2019',
                                  14: '26.10.2019',
                                  15: '27.10.2019',
                                  16: '28.10.2019',
                                  17: '29.10.2019',
                                  18: '30.10.2019',
                                  19: '31.10.2019',
                                  20: '01.11.2019',
                                  21: '02.11.2019',
                                  22: '03.11.2019',
                                  23: '04.11.2019',
                                  24: '05.11.2019',
                                  25: '06.11.2019',
                                  26: '07.11.2019',
                                  27: '08.11.2019',
                                  28: '09.11.2019',
                                  29: '10.11.2019',
                                  30: '11.11.2019',
                                  31: '12.11.2019',
                                  32: '13.11.2019',
                                  33: '14.11.2019',
                                  34: '15.11.2019',
                                  35: '16.11.2019',
                                  36: '17.11.2019',
                                  37: '18.11.2019',
                                  38: '19.11.2019',
                                  39: '20.11.2019',
                                  40: '21.11.2019',
                                  41: '22.11.2019',
                                  42: '23.11.2019',
                                  43: '24.11.2019',
                                  44: '25.11.2019',
                                  45: '26.11.2019',
                                  46: '27.11.2019',
                                  47: '28.11.2019',
                                  48: '29.11.2019',
                                  49: '30.11.2019',
                                  50: '01.12.2019',
                                  51: '02.12.2019',
                                  52: '03.12.2019',
                                  53: '04.12.2019',
                                  54: '05.12.2019',
                                  55: '06.12.2019',
                                  56: '07.12.2019',
                                  57: '08.12.2019',
                                  58: '09.12.2019',
                                  59: '10.12.2019',
                                  60: '11.12.2019',
                                  61: '12.12.2019',
                                  62: '13.12.2019',
                                  63: '14.12.2019',
                                  64: '15.12.2019',
                                  65: '16.12.2019',
                                  66: '17.12.2019',
                                  67: '18.12.2019',
                                  68: '19.12.2019',
                                  69: '20.12.2019',
                                  70: '21.12.2019',
                                  71: '22.12.2019',
                                  72: '23.12.2019',
                                  73: '24.12.2019',
                                  74: '25.12.2019',
                                  75: '26.12.2019',
                                  76: '27.12.2019',
                                  77: '28.12.2019',
                                  78: '29.12.2019',
                                  79: '30.12.2019',
                                  80: '31.12.2019',
                                  81: '01.01.2020',
                                  82: '02.01.2020',
                                  83: '03.01.2020',
                                  84: '04.01.2020',
                                  85: '05.01.2020',
                                  86: '06.01.2020',
                                  87: '07.01.2020',
                                  88: '08.01.2020',
                                  89: '09.01.2020',
                                  90: '10.01.2020',
                                  91: '11.01.2020',
                                  92: '12.01.2020',
                                  93: '13.01.2020',
                                  94: '14.01.2020',
                                  95: '15.01.2020',
                                  96: '16.01.2020',
                                  97: '17.01.2020',
                                  98: '18.01.2020',
                                  99: '19.01.2020',
                                  100: '20.01.2020',
                                  101: '21.01.2020',
                                  102: '22.01.2020',
                                  103: '23.01.2020',
                                  104: '24.01.2020',
                                  105: '25.01.2020',
                                  106: '26.01.2020',
                                  107: '27.01.2020',
                                  108: '28.01.2020',
                                  109: '29.01.2020',
                                  110: '30.01.2020',
                                  111: '31.01.2020'},
                                 'count': {0: 19,
                                  1: 12,
                                  2: 13,
                                  3: 18,
                                  4: 13,
                                  5: 19,
                                  6: 15,
                                  7: 14,
                                  8: 12,
                                  9: 6,
                                  10: 15,
                                  11: 15,
                                  12: 12,
                                  13: 17,
                                  14: 13,
                                  15: 14,
                                  16: 11,
                                  17: 11,
                                  18: 11,
                                  19: 9,
                                  20: 14,
                                  21: 15,
                                  22: 11,
                                  23: 13,
                                  24: 14,
                                  25: 14,
                                  26: 16,
                                  27: 16,
                                  28: 17,
                                  29: 13,
                                  30: 14,
                                  31: 14,
                                  32: 12,
                                  33: 6,
                                  34: 14,
                                  35: 12,
                                  36: 16,
                                  37: 15,
                                  38: 19,
                                  39: 18,
                                  40: 17,
                                  41: 17,
                                  42: 17,
                                  43: 17,
                                  44: 19,
                                  45: 15,
                                  46: 20,
                                  47: 21,
                                  48: 19,
                                  49: 18,
                                  50: 22,
                                  51: 21,
                                  52: 21,
                                  53: 18,
                                  54: 21,
                                  55: 23,
                                  56: 22,
                                  57: 17,
                                  58: 25,
                                  59: 28,
                                  60: 24,
                                  61: 26,
                                  62: 23,
                                  63: 23,
                                  64: 22,
                                  65: 26,
                                  66: 25,
                                  67: 24,
                                  68: 24,
                                  69: 24,
                                  70: 24,
                                  71: 27,
                                  72: 26,
                                  73: 28,
                                  74: 28,
                                  75: 29,
                                  76: 34,
                                  77: 31,
                                  78: 38,
                                  79: 37,
                                  80: 34,
                                  81: 45,
                                  82: 43,
                                  83: 44,
                                  84: 49,
                                  85: 47,
                                  86: 54,
                                  87: 49,
                                  88: 57,
                                  89: 62,
                                  90: 65,
                                  91: 55,
                                  92: 67,
                                  93: 69,
                                  94: 72,
                                  95: 45,
                                  96: 89,
                                  97: 87,
                                  98: 90,
                                  99: 121,
                                  100: 140,
                                  101: 173,
                                  102: 163,
                                  103: 171,
                                  104: 183,
                                  105: 165,
                                  106: 189,
                                  107: 201,
                                  108: 230,
                                  109: 290,
                                  110: 311,
                                  111: 321}})
                Y=np.log(df['count'])
                X=df.index
                
                # log regression
                
                df_log=pd.DataFrame({'X':df.index,
                                     'Y': np.log(df['count'])})
                df_log.set_index('X', inplace = True)
                
                reg = LinearRegression().fit(np.vstack(df_log.index), df_log['Y'])
                df_log['bestfit'] = reg.predict(np.vstack(df_log.index))
                
                df_new=pd.DataFrame({'X':df.index,
                                     'Y':np.exp(df['count']),
                                     'trend':np.exp(df_log['bestfit'])})
                
                df_new.set_index('X', inplace=True)
                
                # plotly figure setup
                fig=go.Figure()
                fig.add_trace(go.Bar(name='X vs Y', x=df_new.index, y=df['count']))
                fig.add_trace(go.Scatter(name='line of best fit', x=df_new.index, y=df_new['trend'], mode='lines'))
                
                # plotly figure layout
                fig.update_layout(xaxis_title = 'X', yaxis_title = 'Y')
                
                fig.show()
                

                这篇关于Ploly:如何在条形图中添加趋势线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                <tfoot id='rxzcQ'></tfoot>
              • <i id='rxzcQ'><tr id='rxzcQ'><dt id='rxzcQ'><q id='rxzcQ'><span id='rxzcQ'><b id='rxzcQ'><form id='rxzcQ'><ins id='rxzcQ'></ins><ul id='rxzcQ'></ul><sub id='rxzcQ'></sub></form><legend id='rxzcQ'></legend><bdo id='rxzcQ'><pre id='rxzcQ'><center id='rxzcQ'></center></pre></bdo></b><th id='rxzcQ'></th></span></q></dt></tr></i><div id='rxzcQ'><tfoot id='rxzcQ'></tfoot><dl id='rxzcQ'><fieldset id='rxzcQ'></fieldset></dl></div>
                1. <small id='rxzcQ'></small><noframes id='rxzcQ'>

                    <legend id='rxzcQ'><style id='rxzcQ'><dir id='rxzcQ'><q id='rxzcQ'></q></dir></style></legend>

                        <tbody id='rxzcQ'></tbody>
                      • <bdo id='rxzcQ'></bdo><ul id='rxzcQ'></ul>