【问题标题】:Plotting the relation between two columns using matplotlib or seaborn使用 matplotlib 或 seaborn 绘制两列之间的关系
【发布时间】:2018-02-13 06:33:33
【问题描述】:

我的数据框中有两列。日期格式的列之一。另一列有 1's 和 0's 。我想绘制一个图表来显示两列之间的关系。这是我的数据的一点 sn-p

Date received   Consumer disputed?
15-05-2014  0
18-09-2014  0
13-03-2014  0
17-07-2015  1
20-11-2014  0
26-06-2014  0
28-09-2012  0
06-05-2015  1
25-02-2013  0
30-03-2016  0
21-03-2014  0

情节应该是 1 和 0 相对于日期的分布,特别是月份部分,这样我就可以决定哪个月份有更多的 1,哪个月份有更多的 0。提前致谢

【问题讨论】:

  • 如果没有出现月份,是否意味着该值为0?
  • 没有月份出现在每一行中。假设 1 和 0 是随机分布的。在某些月份有更多的 1,而在其他月份则更少。使用该图我需要确定一年中哪个部分有更多的消费者争议(1 表示是。0 表示否在消费者争议栏中)

标签: python-3.x pandas matplotlib seaborn


【解决方案1】:

类似...

import matplotlib.pyplot as plt
% matplotlib inline
df['Date'] = pd.to_datetime(df['Date'])
x = df['Date'].values
y = df['received'].values
plt.scatter(x,y)
plt.show()

【讨论】:

    【解决方案2】:

    我会使用条形图

    df['Consumer disputed?'].groupby(df['Date received'].dt.month).sum().plot.bar()
    

    【讨论】:

      【解决方案3】:

      你可能会使用 Seaborn 的联合图

      data['month'] = pd.to_datetime(data['Date']).dt.month
      sns.jointplot(x='Consumer',y='month',data=data)]
      

      【讨论】:

      • 它不工作。显示错误“ValueError:第一个参数必须是一个序列”
      • 你能检查列的数据类型并确保它是 int64 吗?
      【解决方案4】:

      这是我的问题的解决方案。

      #extract the month form the date
      
      train_data['month'] = pd.to_datetime(train_data['Date received']).dt.month
      
      #crosstab displays the frequency distribution of the variable
      #(here "Consumer  disputed?") in a matrix format` 
      b = pd.crosstab(train_data['month'], train_data['Consumer disputed?'])
      
      
      #transform the label month into a column
      b.reset_index(level='month', inplace=True)
      

      #plot the graph
      b.plot('month', 'Yes')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-22
        • 2018-11-23
        • 2018-03-29
        • 2019-10-21
        • 2019-07-13
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        相关资源
        最近更新 更多