您好,登錄后才能下訂單哦!
Python中openpyxl如何實現坐標軸范圍和對數縮放?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
坐標軸最小和最大值
為了在圖表上顯示特定區域,可以手動設置坐標軸的最小值和最大值。
from openpyxl import Workbook from openpyxl.chart import ( ScatterChart, Reference, Series, ) wb = Workbook() ws = wb.active ws.append(['X', '1/X']) for x in range(-10, 11): if x: ws.append([x, 1.0 / x]) chart1 = ScatterChart() chart1.title = "Full Axes" chart1.x_axis.title = 'x' chart1.y_axis.title = '1/x' chart1.legend = None chart2 = ScatterChart() chart2.title = "Clipped Axes" chart2.x_axis.title = 'x' chart2.y_axis.title = '1/x' chart2.legend = None chart2.x_axis.scaling.min = 0 chart2.y_axis.scaling.min = 0 chart2.x_axis.scaling.max = 11 chart2.y_axis.scaling.max = 1.5 x = Reference(ws, min_col=1, min_row=2, max_row=22) y = Reference(ws, min_col=2, min_row=2, max_row=22) s = Series(y, xvalues=x) chart1.append(s) chart2.append(s) ws.add_chart(chart1, "C1") ws.add_chart(chart2, "C15") wb.save("minmax.xlsx")
在某些情況下,如上面代碼所示,設置坐標軸范圍實際上等同于顯示數據的子范圍。對于大型數據集,使用Excel或者Open/Libre Office來繪制散點圖(可能還有其他)時,選擇數據子集方式要比設置坐標軸范圍的速度更快。
對數縮放
x軸和y軸都可以對數縮放。對數的基可以設置為任何有效的浮點。如果x軸按對數縮放,則將丟棄區域中的負值。
from openpyxl import Workbook from openpyxl.chart import ( ScatterChart, Reference, Series, ) import math wb = Workbook() ws = wb.active ws.append(['X', 'Gaussian']) for i, x in enumerate(range(-10, 11)): ws.append([x, "=EXP(-(($A${row}/6)^2))".format(row = i + 2)]) chart1 = ScatterChart() chart1.title = "No Scaling" chart1.x_axis.title = 'x' chart1.y_axis.title = 'y' chart1.legend = None chart2 = ScatterChart() chart2.title = "X Log Scale" chart2.x_axis.title = 'x (log10)' chart2.y_axis.title = 'y' chart2.legend = None chart2.x_axis.scaling.logBase = 10 chart3 = ScatterChart() chart3.title = "Y Log Scale" chart3.x_axis.title = 'x' chart3.y_axis.title = 'y (log10)' chart3.legend = None chart3.y_axis.scaling.logBase = 10 chart4 = ScatterChart() chart4.title = "Both Log Scale" chart4.x_axis.title = 'x (log10)' chart4.y_axis.title = 'y (log10)' chart4.legend = None chart4.x_axis.scaling.logBase = 10 chart4.y_axis.scaling.logBase = 10 chart5 = ScatterChart() chart5.title = "Log Scale Base e" chart5.x_axis.title = 'x (ln)' chart5.y_axis.title = 'y (ln)' chart5.legend = None chart5.x_axis.scaling.logBase = math.e chart5.y_axis.scaling.logBase = math.e x = Reference(ws, min_col=1, min_row=2, max_row=22) y = Reference(ws, min_col=2, min_row=2, max_row=22) s = Series(y, xvalues=x) chart1.append(s) chart2.append(s) chart3.append(s) chart4.append(s) chart5.append(s) ws.add_chart(chart1, "C1") ws.add_chart(chart2, "I1") ws.add_chart(chart3, "C15") ws.add_chart(chart4, "I15") ws.add_chart(chart5, "F30") wb.save("log.xlsx")
這將生成五個類似的圖表:
五張圖使用了相同的數據。其中,第一個圖未縮放,第二和三張圖分別縮放了X和Y軸,第四張圖XY軸均進行了縮放,對數基數設置為10;最后的圖表XY軸均進行了縮放,但對數的底設置為e。
軸線方向
坐標軸可以正常顯示,也可以反向顯示。
軸方向由orientation屬性控制,minMax表示正向,maxMin表示反向。
from openpyxl import Workbook from openpyxl.chart import ( ScatterChart, Reference, Series, ) wb = Workbook() ws = wb.active ws["A1"] = "Archimedean Spiral" ws.append(["T", "X", "Y"]) for i, t in enumerate(range(100)): ws.append([t / 16.0, "=$A${row}*COS($A${row})".format(row = i + 3), "=$A${row}*SIN($A${row})".format(row = i + 3)]) chart1 = ScatterChart() chart1.title = "Default Orientation" chart1.x_axis.title = 'x' chart1.y_axis.title = 'y' chart1.legend = None chart2 = ScatterChart() chart2.title = "Flip X" chart2.x_axis.title = 'x' chart2.y_axis.title = 'y' chart2.legend = None chart2.x_axis.scaling.orientation = "maxMin" chart2.y_axis.scaling.orientation = "minMax" chart3 = ScatterChart() chart3.title = "Flip Y" chart3.x_axis.title = 'x' chart3.y_axis.title = 'y' chart3.legend = None chart3.x_axis.scaling.orientation = "minMax" chart3.y_axis.scaling.orientation = "maxMin" chart4 = ScatterChart() chart4.title = "Flip Both" chart4.x_axis.title = 'x' chart4.y_axis.title = 'y' chart4.legend = None chart4.x_axis.scaling.orientation = "maxMin" chart4.y_axis.scaling.orientation = "maxMin" x = Reference(ws, min_col=2, min_row=2, max_row=102) y = Reference(ws, min_col=3, min_row=2, max_row=102) s = Series(y, xvalues=x) chart1.append(s) chart2.append(s) chart3.append(s) chart4.append(s) ws.add_chart(chart1, "D1") ws.add_chart(chart2, "J1") ws.add_chart(chart3, "D15") ws.add_chart(chart4, "J15") wb.save("orientation.xlsx")
這將生成四個圖表,其中每個可能的方向組合的軸如下所示:
感謝各位的閱讀!看完上述內容,你們對Python中openpyxl如何實現坐標軸范圍和對數縮放大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。