本文介绍了来自Pandas DataFrame的烛台图中的重叠日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据帧输出,如下所示
I have a pandas dataframe output as follows
Open High Low Close
2016-06-01 69.60 70.20 69.44 69.76
2016-06-02 70.00 70.15 69.45 69.54
2016-06-03 69.51 70.48 68.62 68.91
2016-06-04 69.51 70.48 68.62 68.91
2016-06-05 69.51 70.48 68.62 68.91
2016-06-06 70.49 71.44 69.84 70.11
我使用以下代码制作烛台图:
I've used the following code to make the candlestick plot:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
import datetime as dt
#Reset the index to remove Date column from index
df_ohlc = df.reset_index()
#Naming columns
df_ohlc.columns = ["Date","Open","High",'Low',"Close"]
#Converting dates column to float values
df_ohlc['Date'] = df_ohlc['Date'].map(mdates.date2num)
#Making plot
fig = plt.figure()
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=6, colspan=1)
#Converts raw mdate numbers to dates
ax1.xaxis_date()
plt.xlabel("Date")
print(df_ohlc)
#Making candlestick plot
candlestick_ohlc(ax1,df_ohlc.values,width=1, colorup='g', colordown='k',alpha=0.75)
plt.ylabel("Price")
plt.legend()
plt.show()
我得到一个烛台图,但日期重叠,我想知道如何解决此问题?而且我想知道为什么传说没有出现.
I get a candlestick plot but the dates overlap, I want to know how to fix this issue? Moreover I want to know why the legend is not showing up.
推荐答案
以下代码解决了数据重叠的问题
The following code resolves data overlapping issue
fig.autofmt_xdate()
这篇关于来自Pandas DataFrame的烛台图中的重叠日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!