上图的数据是:
>>> plot_data
[[1901, 0.41391], [1902, -0.58147], [1903, -0.52587], [1904, -0.48694], [1905, 1.23691], [1906, -0.55923], [1907, -0.12549], [1908, 1.35925], [1909, 0.82541], [1910, 0.44728], [1911, 1.01448], [1912, 0.55293], [1913, -0.58147], [1914, -0.58147], [1915, 0.91439], [1916, -0.58147], [1917, -0.57591], [1918, -0.18666], [1919, -0.24783], [1920, -0.00315], [1921, 1.34257], [1922, -0.24783], [1923, -0.58147], [1924, 0.49732], [1925, -0.58147], [1926, -0.35904], [1927, -0.58147], [1928, -0.15329], [1929, -0.54811], [1930, 1.72626], [1931, -0.58147], [1932, -0.58147], [1933, -0.57035], [1934, 0.94219], [1935, 1.00336], [1936, -0.53699], [1937, -0.58147], [1938, -0.51474], [1939, -0.54811], [1940, 0.4862], [1941, 0.13031], [1942, 0.13587], [1943, 1.58724], [1944, 0.3583], [1945, 0.45284], [1946, -0.58147], [1947, -0.44801], [1948, 1.17574], [1949, -0.58147], [1950, -0.53699], [1951, -0.49806], [1952, -0.55923], [1953, 1.03116], [1954, -0.47026], [1955, 0.02465], [1956, -0.57591], [1957, 2.03767], [1958, -0.53143], [1959, -0.35348], [1960, -0.53699], [1961, 0.56405], [1962, 0.89214], [1963, -0.17553], [1964, -0.58147], [1965, -0.58147], [1966, -0.58147], [1967, -0.58147], [1968, 0.85322], [1969, -0.57035], [1970, -0.54811], [1971, -0.42021], [1972, 0.30826], [1973, -0.37016], [1974, -0.57035], [1975, -0.52587], [1976, -0.58147], [1977, 0.06358], [1978, -0.58147], [1979, 0.08026], [1980, -0.58147], [1981, -0.35904], [1982, 0.21372], [1983, 0.32494], [1984, -0.52031], [1985, 0.21372], [1986, -0.19778], [1987, -0.25895], [1988, -0.38685], [1989, -0.42021], [1990, -0.58147], [1991, 8.16013], [1992, -0.13105], [1993, -0.58147], [1994, 1.11457], [1995, 0.70307], [1996, -0.58147], [1997, -0.58147], [1998, -0.58147], [1999, -0.15329], [2000, -0.15329], [2001, -0.54811], [2002, 0.3305], [2003, 0.36386], [2004, -0.2089], [2005, -0.58147], [2006, -0.58147], [2007, -0.58147], [2008, -0.58147], [2009, -0.58147], [2010, 0.33606], [2011, -0.58147], [2012, 0.77536], [2013, -0.33124]]
显示上图的代码如下:
import arcpy
from openpyxl import Workbook
from openpyxl.chart import BarChart, Series, Reference
wb = Workbook()
ws = wb.create_sheet()
for row in plot_data:
ws.append(row)
chart1 = BarChart()
chart1.type = "col"
chart1.style = 10
chart1.title = "Rainfall anomaly"
chart1.y_axis.title = "Rainfall"
chart1.x_axis.title = "Years"
chart1.y_axis.scaling.min = -3
chart1.y_axis.scaling.max = 3
dataxl = Reference(ws, min_col=2, min_row=1, max_row=data.shape[0], max_col=2)
cats = Reference(ws, min_col=1, min_row=1, max_row=data.shape[0])
chart1.add_data(dataxl)
chart1.set_categories(cats)
ws.add_chart(chart1, "A10")
wb.save("D:/RainfallAnomaly/PythonScript/ScriptOutput/testingv11.xlsx")
我希望输出看起来像下面这样:
答案 0 :(得分:5)
这是一个有趣的问题。在Excel中格式化图表有很多可能性,我们无法在文档中涵盖它们。但是,openpyxl确实暴露了格式的几乎所有可能性。因此,只要您准备花一点时间查看图表的来源,那么您应该能够使用openpyxl创建类似的图表。
首先要做的事情是:x轴。在Excel中格式化轴非常奇怪。首先,我首先查询了如何执行此操作,因为提供的选项并未表明它。一旦你知道这一点,就很容易了:
chart1.x_axis.tickLblPos = "low"
chart1.x_axis.tickLblSkip = 3 # whatever you like
其次,强调负面价值观。这是使用“invertIfNegative”属性完成的。可以格式化单个系列甚至单个数据点。系列是通过图表的系列对象中的索引访问的。
chart1.series [0] .invertIfNegative = True
但是,对负值使用第二种颜色需要扩展规范,openpyxl目前不支持这些。或者确实是许多客户端(例如,在Excel 2011 for Mac中设置此选项是不可能的,尽管它可以读取它。相关的XML,供参考。几乎所有这些都是注册扩展名“invertSolidFillFmt”。
<c:extLst xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart">
<c:ext xmlns:c14="http://schemas.microsoft.com/office/drawing/2007/8/2/chart" uri="{6F2FDCE9-48DA-4B69-8628-5D25D57E5C99}">
<c14:invertSolidFillFmt>
<c14:spPr xmlns:c14="http://schemas.microsoft.com/office/drawing/2007/8/2/chart">
<a:solidFill xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:srgbClr val="FF0000" />
</a:solidFill>
</c14:spPr>
</c14:invertSolidFillFmt>
</c:ext>
</c:extLst>