用python画蜡烛_如何在python中绘制烛台

I´m trying to create a simple plot with candlesticks. For that I get the data from Yahoo and plot it using the function candlestick2_ohlc. The goal is to export the image in a jpg file using.

This is the code what I´m using:

from pandas_datareader import data

import matplotlib.pyplot as plt

from mpl_finance import candlestick2_ohlc

import matplotlib.dates as mdates

import fix_yahoo_finance as yf

import datetime

start = datetime.date(2018, 1, 1)

end = datetime.date.today()

aapl = yf.download("AAPL",start,end)

aapl.reset_index(inplace=True)

aapl['Date'] = aapl.index.map(mdates.date2num)

fig, ax = plt.subplots()

plt.xlabel("Date")

plt.ylabel("Price")

candlestick2_ohlc(ax, aapl.Open, aapl.High, aapl.Low, aapl.Close, width=1, colorup='g')

plt.savefig('my_figure.png')

plt.show()

My first question is: there is another simple way to do it? Could you please give me an example to work with finance data? I usually work with quantmod in R.

The second question is: In my example, there is no Date in the X axi. What can I do to show the plot with Dates in the X axi? I should transform the Date into a AX format but I don't know a simple way to do it.

Thanks

解决方案

First you need to install the plotly package using:

pip install plotly

Then, you can plot the candle plots as easy as the following code:

import plotly.graph_objects as go

import pandas as pd

from datetime import datetime

df = pd.read_csv('your_file_address')

fig = go.Figure(data=[go.Candlestick(x=df['name_of_time_column'],

open=df['name_of_open_column'],

high=df['name_of_high_column'],

low=df['name_of_low_column'],

close=df['name_of_close_column'])])

fig.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值