除非更改颜色,否则 `xlsxwriter` 线条透明度不起作用
`xlsxwriter` Line Transparency does not work unless color is changed
从文档中我看到可以对图表区域使用透明度。我想知道 XlsxWriter 是否支持折线图的透明度。语法:
chart.add_series({
'name': 'something',
'categories': [<excel range>],
'values': [<excel range>],
'line': {'color': 'yellow','transparency': 25},
})
以黄色显示我的折线图。但是,没有透明度。
那么,折线图可以透明吗?如果是这样,正确的语法是什么?
我 运行 Excel 2016 年 Python 3.6.3。也许值得一提的是我如何使用 xlsxwriter:
writer = pd.ExcelWriter(path, engine='xlsxwriter', datetime_format='dd-mmm-yy')
更新:
运行 已接受答案中的代码产生了所需的输出:
但是,如果线条构造中没有使用颜色参数,则透明度将不起作用:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
# Create a new Chart object.
chart = workbook.add_chart({'type': 'line'})
# Write some data to add to plot on the chart.
data = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
]
worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])
# Configure the charts. In simplest case we just add some data series.
chart.add_series({'values': '=Sheet1!$A:$A'})
chart.add_series({'values': '=Sheet1!$B:$B'})
chart.add_series({'values': '=Sheet1!$C:$C',
'line': {'width':0.25,'transparency': 25}})
# Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart)
workbook.close()
作为检查,我用宽度参数替换了颜色参数,实际上我们的线条显示了正确的宽度,但没有透明度:
未记录但受支持:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
# Create a new Chart object.
chart = workbook.add_chart({'type': 'line'})
# Write some data to add to plot on the chart.
data = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
]
worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])
# Configure the charts. In simplest case we just add some data series.
chart.add_series({'values': '=Sheet1!$A:$A'})
chart.add_series({'values': '=Sheet1!$B:$B'})
chart.add_series({'values': '=Sheet1!$C:$C',
'line': {'color': 'yellow',
'transparency': 25}})
# Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart)
workbook.close()
输出(通过黄线可以看到部分网格线):
更新:现在是documented。
更新二:
however, if there is no color parameter used in the line construction, the transparency will not work:
这是 Excel 中的默认行为。如果不指定颜色,则无法打开透明度。这是因为透明度在内部存储为颜色的 alpha 值的百分比,例如:
<a:ln>
<a:solidFill>
<a:srgbClr val="4F81BD">
<a:alpha val="50000"/>
</a:srgbClr>
</a:solidFill>
</a:ln>
这在 Excel 2015 及更高版本的界面中并不明显,尽管您可以看到当您更改透明度时线条从 "Automatic" 变为 "Solid Line"。在旧版本的 Excel 中,更清楚的是您必须在更改透明度之前指定纯色。
我将更新 XlsxWriter 文档以突出显示这一点。作为解决方法,用户应在更改透明度时指定颜色。
从文档中我看到可以对图表区域使用透明度。我想知道 XlsxWriter 是否支持折线图的透明度。语法:
chart.add_series({
'name': 'something',
'categories': [<excel range>],
'values': [<excel range>],
'line': {'color': 'yellow','transparency': 25},
})
以黄色显示我的折线图。但是,没有透明度。
那么,折线图可以透明吗?如果是这样,正确的语法是什么?
我 运行 Excel 2016 年 Python 3.6.3。也许值得一提的是我如何使用 xlsxwriter:
writer = pd.ExcelWriter(path, engine='xlsxwriter', datetime_format='dd-mmm-yy')
更新:
运行 已接受答案中的代码产生了所需的输出:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
# Create a new Chart object.
chart = workbook.add_chart({'type': 'line'})
# Write some data to add to plot on the chart.
data = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
]
worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])
# Configure the charts. In simplest case we just add some data series.
chart.add_series({'values': '=Sheet1!$A:$A'})
chart.add_series({'values': '=Sheet1!$B:$B'})
chart.add_series({'values': '=Sheet1!$C:$C',
'line': {'width':0.25,'transparency': 25}})
# Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart)
workbook.close()
作为检查,我用宽度参数替换了颜色参数,实际上我们的线条显示了正确的宽度,但没有透明度:
未记录但受支持:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
# Create a new Chart object.
chart = workbook.add_chart({'type': 'line'})
# Write some data to add to plot on the chart.
data = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
]
worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])
# Configure the charts. In simplest case we just add some data series.
chart.add_series({'values': '=Sheet1!$A:$A'})
chart.add_series({'values': '=Sheet1!$B:$B'})
chart.add_series({'values': '=Sheet1!$C:$C',
'line': {'color': 'yellow',
'transparency': 25}})
# Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart)
workbook.close()
输出(通过黄线可以看到部分网格线):
更新:现在是documented。
更新二:
however, if there is no color parameter used in the line construction, the transparency will not work:
这是 Excel 中的默认行为。如果不指定颜色,则无法打开透明度。这是因为透明度在内部存储为颜色的 alpha 值的百分比,例如:
<a:ln>
<a:solidFill>
<a:srgbClr val="4F81BD">
<a:alpha val="50000"/>
</a:srgbClr>
</a:solidFill>
</a:ln>
这在 Excel 2015 及更高版本的界面中并不明显,尽管您可以看到当您更改透明度时线条从 "Automatic" 变为 "Solid Line"。在旧版本的 Excel 中,更清楚的是您必须在更改透明度之前指定纯色。
我将更新 XlsxWriter 文档以突出显示这一点。作为解决方法,用户应在更改透明度时指定颜色。