Python 数据帧中X轴上的数据

Python 数据帧中X轴上的数据,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一些数据要绘制。我正在使用下面的代码: import pandas_datareader as web crypto_currency = 'BTC' against_currency = 'USD' start, end = dt.datetime(2012,1,1), dt.datetime.now() df = web.DataReader(f'{crypto_currency}-{against_currency}', 'yahoo', start, end) df.head()

我有一些数据要绘制。我正在使用下面的代码:

import pandas_datareader as web

crypto_currency = 'BTC'
against_currency = 'USD'
start, end = dt.datetime(2012,1,1), dt.datetime.now()

df = web.DataReader(f'{crypto_currency}-{against_currency}', 'yahoo', start, end)
df.head()
数据框看起来像这样

例如,如果要绘制闭合值,请执行以下操作:

import seaborn as sns; sns.set(style="whitegrid")
import matplotlib.pyplot as plt

close_values = df.filter(['Close']).values

plt.figure(figsize=(20,8))
plt.plot(close_values, label=f'Close values')

plt.xlim(0, len(close_values))
plt.title(f'{crypto_currency}-{against_currency} close data')
plt.legend()
plt.show()
图表如下所示:


没关系,但我想在X轴上显示日期。我想我只需要从索引中获取日期值,并将它们添加为X轴,但这是错误的。我是编错了还是概念上错了?我知道这应该很容易,但我不知道怎么做。

我发现matplotlib有时会对日期有点挑剔-在看不到确切的数据格式的情况下,如果它是熊猫格式,你可以先尝试将它们转换为介绍字符串,然后直接设置
xticks

plt.xticks(list(df.index.astype(str)),list(df.index.astype(str)),旋转=30)

我发现matplotlib有时会对日期有点挑剔-在看不到确切的数据格式的情况下,如果它是
熊猫
格式,您可以尝试先将它们转换为介绍字符串,然后直接设置
xtick

plt.xticks(list(df.index.astype(str)),list(df.index.astype(str)),旋转=30)

因为您使用的是
日期
作为索引,所以只需立即绘制
df['Close']

导入seaborn作为sns;sns.set(style=“whitegrid”)
将matplotlib.pyplot作为plt导入
plt.图(figsize=(20,8))
df['Close'].plot(title=f'{crypto_currency}-{from_currency}Close data',label='Close values')
plt.图例(位置='左上')
plt.xlabel(“”)
plt.show()


如果使用
日期
数据作为索引
,则可以搜索年度、月度和每日数据

new_df=df['2020']
新德里
#df['2020-01':'2020-12']--每月
#df['2020-01-01':'2020-12-31']——每日
plt.图(figsize=(20,8))
新的{df['Close'].plot(title=f'2020{crypto_currency}-{from_currency}Close data')
plt.xlabel(“”)
plt.show()

因为您使用的是
日期
作为索引,所以只需立即绘制
df['Close']

导入seaborn作为sns;sns.set(style=“whitegrid”)
将matplotlib.pyplot作为plt导入
plt.图(figsize=(20,8))
df['Close'].plot(title=f'{crypto_currency}-{from_currency}Close data',label='Close values')
plt.图例(位置='左上')
plt.xlabel(“”)
plt.show()


如果使用
日期
数据作为索引
,则可以搜索年度、月度和每日数据

new_df=df['2020']
新德里
#df['2020-01':'2020-12']--每月
#df['2020-01-01':'2020-12-31']——每日
plt.图(figsize=(20,8))
新的{df['Close'].plot(title=f'2020{crypto_currency}-{from_currency}Close data')
plt.xlabel(“”)
plt.show()