问题描述
我正在尝试绘制股票市场图表
时间序列与收盘价以及时间序列与交易量.
不知何故,x 轴显示了 1970 年的时间
以下是图和代码
代码是:
将pandas导入为pd导入 matplotlib.pyplot 作为 plt导入 matplotlib.dates 作为 mdatespd_data = pd.DataFrame(data, columns=['id', 'symbol', 'volume', 'high', 'low', 'open', 'datetime','close','datetime_utc','created_at'])pd_data['DOB'] = pd.to_datetime(pd_data['datetime_utc']).dt.strftime('%Y-%m-%d')pd_data.set_index('DOB')打印(pd_data)打印(pd_data.dtypes)ax=pd_data.plot(x='DOB',y='close',kind = 'line')ax.set_ylabel(价格")#ax.pd_data['volume'].plot(secondary_y=True, kind='bar')ax1=pd_data.plot(y='volume',secondary_y=True, ax=ax,kind='bar')ax1.set_ylabel('卷')# 选择你的 xtick 格式字符串date_fmt = '%d-%m-%y'date_formatter = mdates.DateFormatter(date_fmt)ax1.xaxis.set_major_formatter(date_formatter)# 设置每月定位器ax1.xaxis.set_major_locator(mdates.MonthLocator(interval=1))# 设置日期刻度标签的字体和旋转plt.gcf().autofmt_xdate()plt.show()
也在没有ax=ax
ax=pd_data.plot(x='DOB',y='close',kind = 'line')ax.set_ylabel(价格")ax1=pd_data.plot(y='volume',secondary_y=True,kind='bar')ax1.set_ylabel('卷')
然后价格图正确显示年份,而体积图显示 1970 年
如果我交换它们
ax1=pd_data.plot(y='volume',secondary_y=True,kind='bar')ax1.set_ylabel('卷')ax=pd_data.plot(x='DOB',y='close',kind = 'line')ax.set_ylabel(价格")
现在成交量图正确显示年份,而价格图显示年份为 1970 年
我尝试删除secondary_y 并将bar 更改为line.但没有运气
不知何故,第一个图表后的熊猫数据正在改变年份.
- 我不建议绘制包含如此多条形的条形图.
- 此答案解释了为什么 xtick 标签存在问题,以及如何解决该问题.
- 使用
pandas.DataFrame.plot
绘图,使用.set_major_locator
没有问题 - 在
python 3.8.11
、pandas 1.3.2
、matplotlib 3.4.2
将pandas导入为pd导入 matplotlib.pyplot 作为 plt导入 matplotlib.dates 作为 mdatesimport pandas_datareader as web # conda install -c anaconda pandas-datareader 或 pip install pandas-datareader# 下载数据df = web.DataReader('amzn', data_source='yahoo', start='2015-02-21', end='2021-04-27')# 阴谋ax = df.plot(y='Close', color='magenta', ls='-.', figsize=(10, 6), ylabel='Price ($)')ax1 = df.plot(y='Volume', secondary_y=True, ax=ax, alpha=0.5, rot=0, lw=0.5)ax1.set(ylabel='音量')# 格式date_fmt = '%d-%m-%y'年 = mdates.YearLocator() # 每年yearsFmt = mdates.DateFormatter(date_fmt)ax.xaxis.set_major_locator(年)ax.xaxis.set_major_formatter(yearsFmt)plt.setp(ax.get_xticklabels(), ha=center")plt.show()
- 为什么 OP x-tick 标签是从 1970 年开始的?
- 条形图位置被 0 索引(使用熊猫),0 对应于 1970
- 请参阅
- 使用
plt.bar
,条形图位置根据日期时间编入索引
ax = df.plot(y='Close', color='magenta', ls='-.', figsize=(10, 6), ylabel='价格($)', rot=0)plt.setp(ax.get_xticklabels(), ha=center")打印(ax.get_xticks())ax1 = ax.twinx()ax1.bar(df.index, df.Volume)打印(ax1.get_xticks())date_fmt = '%d-%m-%y'年 = mdates.YearLocator() # 每年yearsFmt = mdates.DateFormatter(date_fmt)ax.xaxis.set_major_locator(年)ax.xaxis.set_major_formatter(yearsFmt)[出去]:[16071.16436. 16801. 17167. 17532. 17897. 18262. 18628.][16071.16436. 16801. 17167. 17532. 17897. 18262. 18628.]
sns.barplot(x=df.index, y=df.Volume, ax=ax1)
的xtick
位置为[ 0 1 2 ... 1553 1554 1555]
,所以条形图和线图没有对齐.
I am trying to draw a stock market graph
timeseries vs closing price and timeseries vs volume.
Somehow the x-axis shows the time in 1970
the following is the graph and the code
The code is:
import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates pd_data = pd.DataFrame(data, columns=['id', 'symbol', 'volume', 'high', 'low', 'open', 'datetime','close','datetime_utc','created_at']) pd_data['DOB'] = pd.to_datetime(pd_data['datetime_utc']).dt.strftime('%Y-%m-%d') pd_data.set_index('DOB') print(pd_data) print(pd_data.dtypes) ax=pd_data.plot(x='DOB',y='close',kind = 'line') ax.set_ylabel("price") #ax.pd_data['volume'].plot(secondary_y=True, kind='bar') ax1=pd_data.plot(y='volume',secondary_y=True, ax=ax,kind='bar') ax1.set_ylabel('Volumne') # Choose your xtick format string date_fmt = '%d-%m-%y' date_formatter = mdates.DateFormatter(date_fmt) ax1.xaxis.set_major_formatter(date_formatter) # set monthly locator ax1.xaxis.set_major_locator(mdates.MonthLocator(interval=1)) # set font and rotation for date tick labels plt.gcf().autofmt_xdate() plt.show()
Also tried the two graphs independently without
ax=ax
ax=pd_data.plot(x='DOB',y='close',kind = 'line') ax.set_ylabel("price") ax1=pd_data.plot(y='volume',secondary_y=True,kind='bar') ax1.set_ylabel('Volumne')
then price graph shows years properly whereas volumen graph shows 1970
And if i swap them
ax1=pd_data.plot(y='volume',secondary_y=True,kind='bar') ax1.set_ylabel('Volumne') ax=pd_data.plot(x='DOB',y='close',kind = 'line') ax.set_ylabel("price")
Now the volume graph shows years properly whereas the price graph shows the years as 1970
I tried removing secondary_y and also changing bar to line. BUt no luck
Somehow pandas Data after first graph is changing the year.
解决方案- I do not advise plotting a bar plot with such a numerous amount of bars.
- This answer explains why there is an issue with the xtick labels, and how to resolve the issue.
- Plotting with
pandas.DataFrame.plot
works without issue with.set_major_locator
- Tested in
python 3.8.11
,pandas 1.3.2
,matplotlib 3.4.2
import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas_datareader as web # conda install -c anaconda pandas-datareader or pip install pandas-datareader # download data df = web.DataReader('amzn', data_source='yahoo', start='2015-02-21', end='2021-04-27') # plot ax = df.plot(y='Close', color='magenta', ls='-.', figsize=(10, 6), ylabel='Price ($)') ax1 = df.plot(y='Volume', secondary_y=True, ax=ax, alpha=0.5, rot=0, lw=0.5) ax1.set(ylabel='Volume') # format date_fmt = '%d-%m-%y' years = mdates.YearLocator() # every year yearsFmt = mdates.DateFormatter(date_fmt) ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) plt.setp(ax.get_xticklabels(), ha="center") plt.show()
- Why are the OP x-tick labels starting from 1970?
- Bar plots locations are being 0 indexed (with pandas), and 0 corresponds to 1970
- See Pandas bar plot changes date format
- Most solutions with bar plots simply reformat the label to the appropriate datetime, however this is cosmetic and will not align the locations between the line plot and bar plot
- Solution 2 of this answer shows how to change the tick locators, but is really not worth the extra code, when
plt.bar
can be used.
print(pd.to_datetime(ax1.get_xticks())) DatetimeIndex([ '1970-01-01 00:00:00', '1970-01-01 00:00:00.000000001', '1970-01-01 00:00:00.000000002', '1970-01-01 00:00:00.000000003', ... '1970-01-01 00:00:00.000001552', '1970-01-01 00:00:00.000001553', '1970-01-01 00:00:00.000001554', '1970-01-01 00:00:00.000001555'], dtype='datetime64[ns]', length=1556, freq=None)
ax = df.plot(y='Close', color='magenta', ls='-.', figsize=(10, 6), ylabel='Price ($)') print(ax.get_xticks()) ax1 = df.plot(y='Volume', secondary_y=True, ax=ax, kind='bar') print(ax1.get_xticks()) ax1.set_xlim(0, 18628.) date_fmt = '%d-%m-%y' years = mdates.YearLocator() # every year yearsFmt = mdates.DateFormatter(date_fmt) ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) [out]: [16071. 16436. 16801. 17167. 17532. 17897. 18262. 18628.] ← ax tick locations [ 0 1 2 ... 1553 1554 1555] ← ax1 tick locations
- With
plt.bar
the bar plot locations are indexed based on the datetime
ax = df.plot(y='Close', color='magenta', ls='-.', figsize=(10, 6), ylabel='Price ($)', rot=0) plt.setp(ax.get_xticklabels(), ha="center") print(ax.get_xticks()) ax1 = ax.twinx() ax1.bar(df.index, df.Volume) print(ax1.get_xticks()) date_fmt = '%d-%m-%y' years = mdates.YearLocator() # every year yearsFmt = mdates.DateFormatter(date_fmt) ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) [out]: [16071. 16436. 16801. 17167. 17532. 17897. 18262. 18628.] [16071. 16436. 16801. 17167. 17532. 17897. 18262. 18628.]
sns.barplot(x=df.index, y=df.Volume, ax=ax1)
hasxtick
locations as[ 0 1 2 ... 1553 1554 1555]
, so the bar plot and line plot did not align.
这篇关于pandas 条形图结合折线图显示了从 1970 年开始的时间轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- 使用
- 请参阅