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

    1. <small id='fG9t2'></small><noframes id='fG9t2'>

        <bdo id='fG9t2'></bdo><ul id='fG9t2'></ul>
        <legend id='fG9t2'><style id='fG9t2'><dir id='fG9t2'><q id='fG9t2'></q></dir></style></legend>

      1. <tfoot id='fG9t2'></tfoot>

        如何在条形图上绘制时间序列数据

        How to line plot timeseries data on a bar plot(如何在条形图上绘制时间序列数据)

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

                  <tbody id='DcnhB'></tbody>

                <tfoot id='DcnhB'></tfoot>

                  本文介绍了如何在条形图上绘制时间序列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下数据框:

                  data = {'date': ['3/24/2020', '3/25/2020', '3/26/2020', '3/27/2020'],
                          'Total1': [133731.9147, 141071.6383, -64629.74024, 647.5360108],
                          'Total2': [133731.9147, 274803.5529, 210173.8127, 210821.3487]}
                  
                  df = pd.DataFrame(data)
                  
                      date        Total1          Total2
                  0   3/24/2020   133731.9147     133731.9147
                  1   3/25/2020   141071.6383     274803.5529
                  2   3/26/2020   -64629.74024    210173.8127
                  3   3/27/2020   647.5360108     210821.3487
                  

                  df.info()为:

                  <class 'pandas.core.frame.DataFrame'>
                  RangeIndex: 4 entries, 0 to 3
                  Data columns (total 3 columns):
                  date      4 non-null object
                  Total1     4 non-null float64
                  Total2    4 non-null float64
                  dtypes: float64(2), object(1)
                  memory usage: 168.0+ bytes
                  

                  total2是total1的累计总数。我想做一个total1的条形图,然后用total2的折线图覆盖它。

                  ax = sns.barplot(x="date",y="NetPL",data=gby)
                  ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
                  

                  这就是我当前用于条形图的内容。

                  我在将日期转换为日期时间后尝试了此操作

                  plt.style.use('ggplot')
                  ax =sns.barplot(x="date", y="Total1", data=df)
                  ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
                  
                  # add lineplot
                  sns.lineplot(x='date', y='Total2', data=df, marker='o')
                  plt.show()
                  

                  推荐答案

                  • 测试于python 3.8.12pandas 1.3.4matplotlib 3.4.3seaborn 0.11.2

                  'date'作为str

                  • 这是可行的,因为条形图的刻度位置是0索引的,而且由于'date'列值是字符串,所以线条图刻度位置也是0索引的。
                    • 使用p1.get_xticklabels()
                    • 检查位置和标签
                  plt.style.use('ggplot')
                  
                  p1 = sns.barplot(x="date", y="Total1", data=df)
                  p1.set_xticklabels(ax.get_xticklabels(), rotation=45)
                  
                  # add lineplot to the same axes
                  p2 = sns.lineplot(data=df, x='date', y='Total2', marker='o', ax=p1)
                  p1.set(ylabel='Total', xlabel='Date')
                  plt.show()
                  

                  'date'作为datetime dtype

                  • p2 = sns.lineplot(data=df, x='date', y='Total2', marker='o')会导致以下xtick位置:
                    • p2.get_xticks()array([18345. , 18345.5, 18346. , 18346.5, 18347. , 18347.5, 18348. ]),与条形图产生的0个索引xtick位置不对应
                  • 根据条形图xtick位置p1.get_xticks()绘制折线图,或使用df.index(),只要索引为0索引RangeIndex
                  # convert date to a datetime dtype and extract only the date component
                  df['date'] = pd.to_datetime(df['date']).dt.date
                  
                  p1 = sns.barplot(data=df, x='date', y='Total1')
                  p1.set_xticklabels(ax.get_xticklabels(), rotation=45)
                  
                  # get the xtick locations
                  xticks = p1.get_xticks()
                  
                  # plot the line to the xtick locs (or df.index)
                  p2 = sns.lineplot(data=df, x=xticks, y='Total2', marker='o', ax=p1)
                  
                  p1.set(ylabel='Total', xlabel='Date')
                  plt.show()
                  

                  这篇关于如何在条形图上绘制时间序列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                    <i id='GEnnY'><tr id='GEnnY'><dt id='GEnnY'><q id='GEnnY'><span id='GEnnY'><b id='GEnnY'><form id='GEnnY'><ins id='GEnnY'></ins><ul id='GEnnY'></ul><sub id='GEnnY'></sub></form><legend id='GEnnY'></legend><bdo id='GEnnY'><pre id='GEnnY'><center id='GEnnY'></center></pre></bdo></b><th id='GEnnY'></th></span></q></dt></tr></i><div id='GEnnY'><tfoot id='GEnnY'></tfoot><dl id='GEnnY'><fieldset id='GEnnY'></fieldset></dl></div>

                    • <small id='GEnnY'></small><noframes id='GEnnY'>

                      • <tfoot id='GEnnY'></tfoot>
                        • <bdo id='GEnnY'></bdo><ul id='GEnnY'></ul>
                            <tbody id='GEnnY'></tbody>

                          1. <legend id='GEnnY'><style id='GEnnY'><dir id='GEnnY'><q id='GEnnY'></q></dir></style></legend>