【问题标题】:How can i plotting two columns with string as value in a DataSet with Matplotlib?如何使用 Matplotlib 在 DataSet 中以字符串为值绘制两列?
【发布时间】:2021-01-12 06:25:55
【问题描述】:

我有以下数据集,我想创建一个图,列相互比较。

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

ds=pd.read_csv('h-t-t-p-:bit.ly/uforeports') #My DataSet
ds.head(5) # Only the fist 5 rows to show

ds1= ds.head(4).drop(['Colors Reported','State'],axis=1) # Droping of unnecesssary rows
print(ds1)

现在我想在绘图的帮助下比较“City”和“Shape Reported”。我在 Pandas 中找到了一些东西,但这不是那么优雅!

x=ds.loc[0:100,['State']]
y=ds.loc[0:100,['Shape Reported']]

x.apply(pd.value_counts).plot(kind='bar', subplots=True)
y.apply(pd.value_counts).plot(kind='bar', subplots=True)

你知道用 Matplotlib 解决这个问题的更好方法吗?

This is what I want

【问题讨论】:

  • 您是否尝试过查看 Matplotlib 的任何教程?他们中的许多人会准确地引导您完成此过程。考虑看看this one
  • 是的,我有。但是,如果您确切地看到本教程,它只解释了数字和文本之间的理解。我想要文字和文字之间的理解!
  • 我知道用 Sklearn.compose 和 ColumnTrasnform 会很简单,但我想用 python 来做
  • 这可能会有所帮助:stackoverflow.com/questions/58303175/…

标签: pandas matplotlib plot dataset


【解决方案1】:

不清楚你想如何比较它们。

绘制条形图最简单的方法是:

df['State'].value_counts().plot.bar()
df['Shape Reported'].value_counts().plot.bar()

如果您只想像示例中那样对前 100 行执行此操作,只需添加 head(100):

df['State'].head(100).value_counts().plot.bar()
df['Shape Reported'].head(100).value_counts().plot.bar()

编辑:

要比较这两个值,您可以绘制双变量分布图。使用 seaborn 很容易做到这一点:

import seaborn
sns.displot(df,x='State', y='Shape Reported', height=6, aspect=1.33)

结果:

【讨论】:

  • 我的意图只是,我想将“状态”列的值作为 X 轴,将“形状报告”列中的值作为 Y 轴。
  • 很遗憾不是我想要的
  • 你能提供一个你想要的情节风格的例子吗?
  • @mullinscr 我已经编辑了我的问题。您可以在我的问题中找到图的链接
猜你喜欢
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多