Matplotlib仅绘制日期,而不是给定的DateTime变量

发布于 2025-02-04 10:56:12 字数 2344 浏览 1 评论 0原文

我正在尝试绘制烛台,但由于某种原因,该图不会抓住整个DateTime变量。但是只有日期。 给出的数据。

将所有蜡烛绘制在相同的X轴上,而不是所有15分钟,因为在代码之后

        ##  PLOT Candles
        plt.figure()

        #define width of candlestick elements
        width = .5
        width2 = .05

        #define up and down prices
        up = df[df.close>=df.open]
        down = df[df.close<df.open]
        print(up)
        #define colors to use
        col1 = 'green'
        col2 = 'red'
        col3 = 'blue'
        col4 = 'grey'

        #plot up prices
        plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
        plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
        plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)

        #plot down prices
        plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
        plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
        plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
        
        plt.show()
        exit()

以下是UP的打印

{'Open':{Timestamp('2016-01-14 08:15:00'):1.08719, 时间戳('2016-01-14 08:30:00'):1.08735,时间戳('2016-01-14 08:45:00'):1.08674,时间戳('2016-01-14 09:00:00'):1.08674, 时间戳('2016-01-14 09:15:00'):1.08671},'High': {Timestamp('2016-01-14 08:15:00'):1.08749,时间戳('2016-01-14 08:30:00'):1.08739,时间戳('2016-01-14 08:45:00'):1.08734, 时间戳('2016-01-14 09:00:00'):1.08722,时间戳('2016-01-14 09:15:00'):1.08673},'low':{timestamp('2016-01-14 08:15:00'): 1.0869,时间戳('2016-01-14 08:30:00'):1.08673,时间戳记('2016-01-14 08:45:00'):1.08669,Timestamp('2016-01-01-14 09:00:00'):1.08666,时间戳('2016-01-14 09:15:00'):1.08582}, “关闭”:{timestamp('2016-01-14 08:15:00'):1.08736, 时间戳('2016-01-14 08:30:00'):1.08673,时间戳('2016-01-14 08:45:00'):1.08673,时间戳('2016-01-14 09:00:00'):1.08671, 时间戳('2016-01-14 09:15:00'):1.08618},'卷': {Timestamp('2016-01-14 08:15:00'):2181,时间戳('2016-01-14 08:30:00'):1738,时间戳('2016-01-14 08:45:00'):1938, 时间戳('2016-01-14 09:00:00'):3010,时间戳('2016-01-14 09:15:00'):2734}}

I am trying to plot candle sticks but for some reason, the plot does not grab the entire DateTime variable. But only the date. Plotting all candles on the same X axis rather than all 15 min as the data is given in.

Following code.

        ##  PLOT Candles
        plt.figure()

        #define width of candlestick elements
        width = .5
        width2 = .05

        #define up and down prices
        up = df[df.close>=df.open]
        down = df[df.close<df.open]
        print(up)
        #define colors to use
        col1 = 'green'
        col2 = 'red'
        col3 = 'blue'
        col4 = 'grey'

        #plot up prices
        plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
        plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
        plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)

        #plot down prices
        plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
        plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
        plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
        
        plt.show()
        exit()

Following is the print of up
enter image description here

enter image description here

print(df[:5].to_dict())

{'open': {Timestamp('2016-01-14 08:15:00'): 1.08719,
Timestamp('2016-01-14 08:30:00'): 1.08735, Timestamp('2016-01-14
08:45:00'): 1.08674, Timestamp('2016-01-14 09:00:00'): 1.08674,
Timestamp('2016-01-14 09:15:00'): 1.08671}, 'high':
{Timestamp('2016-01-14 08:15:00'): 1.08749, Timestamp('2016-01-14
08:30:00'): 1.08739, Timestamp('2016-01-14 08:45:00'): 1.08734,
Timestamp('2016-01-14 09:00:00'): 1.08722, Timestamp('2016-01-14
09:15:00'): 1.08673}, 'low': {Timestamp('2016-01-14 08:15:00'):
1.0869, Timestamp('2016-01-14 08:30:00'): 1.08673, Timestamp('2016-01-14 08:45:00'): 1.08669, Timestamp('2016-01-14
09:00:00'): 1.08666, Timestamp('2016-01-14 09:15:00'): 1.08582},
'close': {Timestamp('2016-01-14 08:15:00'): 1.08736,
Timestamp('2016-01-14 08:30:00'): 1.08673, Timestamp('2016-01-14
08:45:00'): 1.08673, Timestamp('2016-01-14 09:00:00'): 1.08671,
Timestamp('2016-01-14 09:15:00'): 1.08618}, 'volume':
{Timestamp('2016-01-14 08:15:00'): 2181, Timestamp('2016-01-14
08:30:00'): 1738, Timestamp('2016-01-14 08:45:00'): 1938,
Timestamp('2016-01-14 09:00:00'): 3010, Timestamp('2016-01-14
09:15:00'): 2734}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-02-11 10:56:12

好吧,让我们这样做!

第一件事首先:

新的API这个存储库Matplotlib/MplFinance包含一个新的
Matplotlib Finance API使创建财务地块变得更加容易。
它与Pandas DataFrames很好地接口。

更重要的是,新的API会自动执行额外的matplotlib
用户以前必须使用旧API“手动”进行的工作。

pip install --upgrade mplfinance
#or
import mplfinance as mpf

给定样本我问您:

data = {'open': {'2016-01-14 08:15:00': 1.08719, '2016-01-14 08:30:00': 1.08735, '2016-01-14 08:45:00': 1.08674, '2016-01-14 09:00:00': 1.08674, '2016-01-14 09:15:00': 1.08671}, 'high': {'2016-01-14 08:15:00': 1.08749, '2016-01-14 08:30:00': 1.08739, '2016-01-14 08:45:00': 1.08734, '2016-01-14 09:00:00': 1.08722, '2016-01-14 09:15:00': 1.08673}, 'low': {'2016-01-14 08:15:00': 1.0869, '2016-01-14 08:30:00': 1.08673, '2016-01-14 08:45:00': 1.08669, '2016-01-14 09:00:00': 1.08666, '2016-01-14 09:15:00': 1.08582}, 'close': {'2016-01-14 08:15:00': 1.08736, '2016-01-14 08:30:00': 1.08673, '2016-01-14 08:45:00': 1.08673, '2016-01-14 09:00:00': 1.08671, '2016-01-14 09:15:00': 1.08618}, 'volume': {'2016-01-14 08:15:00': 2181, '2016-01-14 08:30:00': 1738, '2016-01-14 08:45:00': 1938, '2016-01-14 09:00:00': 3010, '2016-01-14 09:15:00': 2734}}

将“数据”转换为df,然后将index转换为正确的格式:

df = pd.DataFrame(data)
df.index = pd.to_datetime(df.index)
df

然后绘制它!

mpf.plot(df, type='candle')

输出:

Well, let's do this!

First things first:
https://pypi.org/project/mplfinance/

The New API This repository, matplotlib/mplfinance, contains a new
matplotlib finance API that makes it easier to create financial plots.
It interfaces nicely with Pandas DataFrames.

More importantly, the new API automatically does the extra matplotlib
work that the user previously had to do "manually" with the old API.

pip install --upgrade mplfinance
#or
import mplfinance as mpf

Given the sample I asked you:

data = {'open': {'2016-01-14 08:15:00': 1.08719, '2016-01-14 08:30:00': 1.08735, '2016-01-14 08:45:00': 1.08674, '2016-01-14 09:00:00': 1.08674, '2016-01-14 09:15:00': 1.08671}, 'high': {'2016-01-14 08:15:00': 1.08749, '2016-01-14 08:30:00': 1.08739, '2016-01-14 08:45:00': 1.08734, '2016-01-14 09:00:00': 1.08722, '2016-01-14 09:15:00': 1.08673}, 'low': {'2016-01-14 08:15:00': 1.0869, '2016-01-14 08:30:00': 1.08673, '2016-01-14 08:45:00': 1.08669, '2016-01-14 09:00:00': 1.08666, '2016-01-14 09:15:00': 1.08582}, 'close': {'2016-01-14 08:15:00': 1.08736, '2016-01-14 08:30:00': 1.08673, '2016-01-14 08:45:00': 1.08673, '2016-01-14 09:00:00': 1.08671, '2016-01-14 09:15:00': 1.08618}, 'volume': {'2016-01-14 08:15:00': 2181, '2016-01-14 08:30:00': 1738, '2016-01-14 08:45:00': 1938, '2016-01-14 09:00:00': 3010, '2016-01-14 09:15:00': 2734}}

Transform "data" into a df and convert the index to the right format:

df = pd.DataFrame(data)
df.index = pd.to_datetime(df.index)
df

Then plot it!

mpf.plot(df, type='candle')

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文