1. 程式人生 > >Python第三方庫之openpyxl(11)

Python第三方庫之openpyxl(11)

strong 結合 圖片 import 收盤價 series 運行 -h mar

Python第三方庫之openpyxl(11)

Stock Charts(股票圖)

在工作表上按特定順序排列的列或行中的數據可以在股票圖表中繪制。正如其名稱所暗示的,股票圖表通常被用來說明股價的波動。然而,這張圖表也可以用於科學數據。例如,你可以用一個股票圖表來表示每日或每年的溫度波動。您必須按照正確的順序組織您的數據,以創建股票圖表。

在工作表中組織股票圖表數據是非常重要的。例如,為了創建一個簡單的高低收盤價的股票圖表,您應該按照這個順序將您的數據以高、低和接近的形式排列為列標題。

盡管股票圖表是一種獨特的類型,但各種類型只是特定格式選項的快捷方式:

1.high-low-close本質上是一個沒有線條的折線圖,並且標記為XYZ。它也會使hiLoLines設置為True

2.open-high-low-close是一個高低近距離的圖表,每個數據點的標記都是XZZ和updownline。

可以通過將股票圖表與卷的條形圖相結合來增加卷。

技術分享圖片
from datetime import date

from openpyxl import Workbook

from openpyxl.chart import (
    BarChart,
    StockChart,
    Reference,
    Series,
)
from openpyxl.chart.axis import DateAxis, ChartLines
from openpyxl.chart.updown_bars import
UpDownBars wb = Workbook() ws = wb.active rows = [ [Date, Volume,Open, High, Low, Close], [2015-01-01, 20000, 26.2, 27.20, 23.49, 25.45, ], [2015-01-02, 10000, 25.45, 25.03, 19.55, 23.05, ], [2015-01-03, 15000, 23.05, 24.46, 20.03, 22.42, ], [2015-01-04, 2000, 22.42, 23.97, 20.07, 21.90, ], [
2015-01-05, 12000, 21.9, 23.65, 19.50, 21.51, ], ] for row in rows: ws.append(row) # High-low-close c1 = StockChart() labels = Reference(ws, min_col=1, min_row=2, max_row=6) data = Reference(ws, min_col=4, max_col=6, min_row=1, max_row=6) c1.add_data(data, titles_from_data=True) c1.set_categories(labels) for s in c1.series: s.graphicalProperties.line.noFill = True # marker for close s.marker.symbol = "dot" s.marker.size = 5 c1.title = "High-low-close" c1.hiLowLines = ChartLines() # Excel is broken and needs a cache of values in order to display hiLoLines :-/ from openpyxl.chart.data_source import NumData, NumVal pts = [NumVal(idx=i) for i in range(len(data) - 1)] cache = NumData(pt=pts) c1.series[-1].val.numRef.numCache = cache ws.add_chart(c1, "A10") # Open-high-low-close c2 = StockChart() data = Reference(ws, min_col=3, max_col=6, min_row=1, max_row=6) c2.add_data(data, titles_from_data=True) c2.set_categories(labels) for s in c2.series: s.graphicalProperties.line.noFill = True c2.hiLowLines = ChartLines() c2.upDownBars = UpDownBars() c2.title = "Open-high-low-close" # add dummy cache c2.series[-1].val.numRef.numCache = cache ws.add_chart(c2, "G10") # Create bar chart for volume bar = BarChart() data = Reference(ws, min_col=2, min_row=1, max_row=6) bar.add_data(data, titles_from_data=True) bar.set_categories(labels) from copy import deepcopy # Volume-high-low-close b1 = deepcopy(bar) c3 = deepcopy(c1) c3.y_axis.majorGridlines = None c3.y_axis.title = "Price" b1.y_axis.axId = 20 b1.z_axis = c3.y_axis b1.y_axis.crosses = "max" b1 += c3 c3.title = "High low close volume" ws.add_chart(b1, "A27") ## Volume-open-high-low-close b2 = deepcopy(bar) c4 = deepcopy(c2) c4.y_axis.majorGridlines = None c4.y_axis.title = "Price" b2.y_axis.axId = 20 b2.z_axis = c4.y_axis b2.y_axis.crosses = "max" b2 += c4 ws.add_chart(b2, "G27") wb.save("stock.xlsx")
View Code

註意:由於Excel high-low lines的缺陷,只有在至少一個數據系列有一些虛擬值時才會顯示出來。這可以通過以下的攻擊來完成:

from openpyxl.chart.data_source import NumData, NumVal
pts = [NumVal(idx=i) for i in range(len(data) - 1)]
cache = NumData(pt=pts)
c1.series[-1].val.numRef.numCache = cache

運行結果

技術分享圖片

技術分享圖片

Python第三方庫之openpyxl(11)