提问人:Elio Baharan 提问时间:3/11/2023 最后编辑:Trenton McKinneyElio Baharan 更新时间:3/12/2023 访问量:65
如何绘制重复值的线性图
How to draw linear graph of repetitive values
问:
我有一个 csv 文件,一列的值是重复的,并且有它们的数量。
现在,如何绘制值的线性图?
我这样做了,但它没有用。
import matplotlib.pyplot as plt
import pandas as pd
data = {'location': ['Afghanistan'] * 5 + ['Africa'] * 4, 'new_cases': [3, 0, 0, 3, 6, 0, 1, 0, 0]}
newData = pd.DataFrame(data)
fig, ax = plt.subplots(figsize=(15,7))
byLoc = newData.groupby('location').count()['new_cases'].unstack().plot(ax=ax)
追踪
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [141], line 2
1 fig, ax = plt.subplots(figsize=(15,7))
----> 2 byLoc = newData.groupby('location').count()['new_cases'].unstack().plot(ax=ax)
File ~\anaconda3\envs\py11\Lib\site-packages\pandas\core\series.py:4455, in Series.unstack(self, level, fill_value)
4412 """
4413 Unstack, also known as pivot, Series with MultiIndex to produce DataFrame.
4414
(...)
4451 b 2 4
4452 """
4453 from pandas.core.reshape.reshape import unstack
-> 4455 return unstack(self, level, fill_value)
File ~\anaconda3\envs\py11\Lib\site-packages\pandas\core\reshape\reshape.py:483, in unstack(obj, level, fill_value)
478 return obj.T.stack(dropna=False)
479 elif not isinstance(obj.index, MultiIndex):
480 # GH 36113
481 # Give nicer error messages when unstack a Series whose
482 # Index is not a MultiIndex.
--> 483 raise ValueError(
484 f"index must be a MultiIndex to unstack, {type(obj.index)} was passed"
485 )
486 else:
487 if is_1d_only_ea_dtype(obj.dtype):
ValueError: index must be a MultiIndex to unstack, <class 'pandas.core.indexes.base.Index'> was passed
答:
0赞
Elkhan
3/11/2023
#1
对于绘制折线图,您必须使用时间属性。我假设你有时间属性,这条线显示了一段时间内的新案例。
import matplotlib.pyplot as plt
plt.plot(df['time'], df['new_cases'])
plt.title('New Cases over Time')
plt.xlabel('Time')
plt.ylabel('New Cases')
plt.show()
为了显示新案例和位置的相对性,您可以使用更合适的条形图。
2赞
Trenton McKinney
3/12/2023
#2
导入和 DataFrame
import pandas as pd
df = pd.DataFrame({'location': ['Afghanistan'] * 5 + ['Africa'] * 4, 'new_cases': [3, 0, 0, 3, 6, 0, 1, 0, 0]})
绘制新案例
# pivot and drop nan
dfp = df.pivot(columns='location', values='new_cases').apply(lambda x: pd.Series(x.dropna().values))
# plot
ax = dfp.plot(figsize=(8, 6), title='New Cases', xticks=dfp.index)
绘制累积新案例
# add a cumulative column
df['cumulative'] = df.groupby('location').new_cases.transform('cumsum')
# pivot and drop nan
dfp = df.pivot(columns='location', values='cumulative').apply(lambda x: pd.Series(x.dropna().values))
# plot
ax = dfp.plot(figsize=(8, 6), title='New Cases', xticks=dfp.index)
评论