【问题标题】:How can I plot specific attributes rather than default of all attributes in Time Series如何绘制特定属性而不是时​​间序列中所有属性的默认值
【发布时间】:2020-07-09 04:44:07
【问题描述】:

如何绘制时间序列的特定属性,而不是数据框中所有属性的默认值。我想制作一个特定属性和两个特定属性的时间序列。是否可以打开一个员工人数的时间序列图和另一个员工人数和表格的时间序列图?下面是我一直在使用的代码,如果我尝试调用特定变量,我会得到错误代码。提前致谢

# Load necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Load data
filename = 'https://library.startlearninglabs.uw.edu/DATASCI410/Datasets/JitteredHeadCount.csv'
headcount_df = pd.read_csv(filename)
headcount_df.describe()

headcount_df.columns

ax = plt.figure(figsize=(12, 3)).gca() # define axis
headcount_df.plot(ax = ax)
ax.set_xlabel('Date')
ax.set_ylabel('Number of guests')
ax.set_title('Time series of Casino data')

【问题讨论】:

  • 如果我的回答对您有所帮助,我将非常感谢您投赞成票并接受绿色检查。如果您需要更多帮助,请告诉我。

标签: python pandas time-series


【解决方案1】:

您可能不得不弄乱刻度和其他一些格式,但这应该会让您朝着正确的方向前进。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


filename = 'https://library.startlearninglabs.uw.edu/DATASCI410/Datasets/JitteredHeadCount.csv'
headcount_df = pd.read_csv(filename)

headcount_df['DateFormat'] = pd.to_datetime(headcount_df['DateFormat'].fillna('ffill'))
headcount_df.set_index('DateFormat', inplace=True)
headcount_df.sort_index(inplace=True)

headcount_df_to = headcount_df[['TablesOpen']]
headcount_df_hc_to = headcount_df[['HeadCount', 'TablesOpen']]

fig, axes = plt.subplots(nrows=2, ncols=1,
                         figsize=(12, 8))


headcount_df_to.plot(ax=axes[0], color=['orange'])
headcount_df_hc_to.plot(ax=axes[1], color=['blue', 'orange'])


axes[0].set_xlabel('Date')
axes[0].set_ylabel('Tables Open')
axes[0].legend(loc='center left', bbox_to_anchor=(1, 0.5))


axes[1].set_xlabel('Date')
axes[1].set_ylabel('Number of guests and Tables Open')
axes[1].legend(loc='center left', bbox_to_anchor=(1, 0.5))

fig.suptitle('Time Series of Casino data')

【讨论】:

  • 谢谢你,你会推荐什么方法来让图表“可以这么说更分散?现在我遇到的问题是它只是为了从中获取任何信息而变得密集
  • 不客气。 headcount_df 有 175k 行,因此很难在单个图表上看到很多细节。如果您想清除趋势,我建议使用带有日期时间索引的分组。 chrisalbon.com/python/data_wrangling/pandas_group_data_by_time 或绘制更大数据集的切片。 Bokeh 和 Plotly 也非常棒,因为它们允许您放大 docs.bokeh.org/en/latest/docs/gallery/stocks.htmlplotly.com/python/line-charts
  • 我会调查这些。再次感谢!
  • 不客气!感谢您的绿色检查。祝您好运,如果您需要帮助,请随时发布新的具体问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
相关资源
最近更新 更多