有人可以分享一个关于如何使用 Chart.from_pandas 方法在 Jupyter 中绘制烛台图的简单示例吗?我的数据框包含标准日期时间、开盘价、最高价、最低价、收盘价、成交量以及其他一些数据。网站上有一个例子就更好了。我兜了一圈也没找到文档。我确信这将是我(充其量是休闲程序员),但我对许多选择感到相当迷失。提前TY。
我需要创建这个结果: https://www.highcharts.com/demo/stock/candlestick-and-volume
from highcharts_stock.chart import Chart
from highcharts_stock.global_options.shared_options import SharedStockOptions
from highcharts_stock.options import HighchartsStockOptions
#from highcharts_stock.options.plot_options.bar import BarOptions
from highcharts_stock.options.series.bar import BarSeries
import yfinance as yf
import pandas as pd
import datetime as dt
tsla = yf.Ticker('TSLA')
df = tsla.history(period="1y")
df.tail(3)
my_chart = Chart.from_pandas(
df,
property_map={
'date': 'Date', # Prob wrong but cannot find info
'open': 'Open', # Assumed from dict examples.
'high': 'High',
'low': 'Low',
'close': 'Close',
'volume': 'Volume'
},
series_type= Candlestick,
series_kwargs = {
'point_padding': 0.5
},
options_kwargs = {
'title': {
'text': 'This Is My Chart Title'
}
},
chart_kwargs = {
'container': 'my_target_div'
})
my_chart.display()
我是 Chris,Highcharts for Python 的主要作者。您的代码“几乎”正确,但有一个细微的调整。当您提供 series_type
作为参数时,您需要提供系列的类 (
CandlestickSeries
) 或系列类型的名称 (candlestick
)。所以你可以使用以下代码:
from highcharts_stock.chart import Chart
from highcharts_stock.global_options.shared_options import SharedStockOptions
from highcharts_stock.options import HighchartsStockOptions
from highcharts_stock.options.series.candlestick import CandlestickSeries
import yfinance as yf
import pandas as pd
import datetime as dt
tsla = yf.Ticker('TSLA')
df = tsla.history(period="1y")
df.tail(3)
my_chart = Chart.from_pandas(
df,
property_map = {
'x': 'Date',
'open': 'Open',
'high': 'High',
'low': 'Low',
'close': 'Close',
},
series_type = CandlestickSeries,
series_kwargs = {
'point_padding': 0.5
},
options_kwargs = {
'title': {
'text': 'This Is My Chart Title'
}
},
chart_kwargs = {
'container': 'my_target_div'
})
my_chart.display()
由于它可能会有所帮助,因此这里有一个指向以下文档的链接:
CandlestickSeries
https://stock-docs.highchartspython.com/en/latest/api/options/series/candlestick.html#highcharts_stock.options.series.candlestick
CandlestickData
https://stock-docs.highchartspython.com/en/latest/api/options/series/data/candlestick.html#highcharts_stock.options.series.data.candlestick.CandlestickData
x
、
high
、low
、open
和 close
属性。如果您希望在烛台系列下方重新创建成交量跟踪器,则需要使用此处描述的 Navigator
功能:https://stock-docs.highchartspython.com/en/latest/using.html#working- with-highcharts-stock-features(基本上,您需要将
Navigator
类添加到图表中,并向 Navigator
类提供特定于交易量的系列)。您可以在此处找到有关配置 Navigator
功能的更多文档:
https://stock-docs.highchartspython.com/en/latest/api/options/navigator.html#highcharts_stock.options.navigator.Navigator和特别是我建议在这里查看
Navigator.series
属性:https://stock-docs.highchartspython.com/en/latest/api/options/navigator.html#highcharts_stock.options.navigator.Navigator.series希望这有帮助!