![](/img/trans.png)
[英]AttributeError: 'int' object has no attribute 'plot' with pie chart
[英]'DataFrame' object has no attribute 'value' to plot a chart
我需要 plot 圖表,但它會引發以下錯誤:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6816/1118115357.py in <module>
5 plt.show()
6
----> 7 plot_df(df, x=df.index, y=df.value, title='Mecerdez Car Sales in the US from 2015 to 2020.')
~\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5485 ):
5486 return self[name]
-> 5487 return object.__getattribute__(self, name)
5488
5489 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'value'
代碼:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.figsize': (10, 7), 'figure.dpi': 120})
%matplotlib inline
df = pd.read_csv('TS.csv', parse_dates = ['date'], index_col = 'date')
def plot_df(df, x, y, title="", xlabel='Date', ylabel='Sales', dpi=100):
plt.figure(figsize=(16,5), dpi=dpi)
plt.plot(x, y, color='tab:red')
plt.gca().set(title=title, xlabel=xlabel, ylabel=ylabel)
plt.show()
plot_df(df, x=df.index, y=df.value, title='Mecerdez Car Sales in the US from 2015 to 2020.')
DataFrame:
Sales
date
2015-01-01 6236.0
2015-01-02 7072.0
2015-01-03 7635.0
2015-01-04 6665.0
2015-01-05 7413.0
嘗試:
plot_df(df, x=df.index, y=df['Sales'], title='Mecerdez Car Sales in the US from 2015 to 2020.')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.