Xlsxwriter 图表标签在饼图中不起作用
Xlsxwriter Chart Lables not working in Pie Chart
我正在使用 python 和 xlsxwriter 在 Excel 文件中创建表格和图表。当我在图表中添加标签时,它们没有显示。
如有任何帮助,我们将不胜感激。
代码:
chart1a.add_series({
'name': '=Totals_FY!$C',
'categories': '=Totals_FY!$B:$B$' + str(types_pivot.shape[0]+3),
'values': '=Totals_FY!$D:$D$' + str(types_pivot.shape[0]+3),
'data_labels': {
'value':True,
'category_name':True,
'position':'outside_end'
})
图表 1a
Type Year 1 Year 2
a 150 69
b 48 25
c 9 0
d 0 1
e 67 44
f 0 3
g 46 19
h 0 1
data_labels
参数中有错字 ('category_name' -> 'category')。除此之外,它应该按预期工作。
这是一个基于您的示例:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Type': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
'Year 1': [150, 48, 9, 0, 67, 0, 46, 0],
'Year 2': [69, 25, 0, 1, 44, 3, 19, 1]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_chart.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Totals_FY', startrow=2)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Totals_FY']
# Create a chart object.
chart = workbook.add_chart({'type': 'pie'})
# Configure the series of the chart from the dataframe data.
chart.add_series({
'name': '=Totals_FY!$C',
'categories': '=Totals_FY!$B:$B$' + str(df.shape[0] + 3),
'values': '=Totals_FY!$D:$D$' + str(df.shape[0] + 3),
'data_labels': {
'value': True,
'category': True,
'position': 'outside_end'}
})
# Insert the chart into the worksheet.
worksheet.insert_chart('F3', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
输出:
请注意,在此类从数据框创建图表的程序中,最好使用 add_series()
:
的列表语法
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Type': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
'Year 1': [150, 48, 9, 0, 67, 0, 46, 0],
'Year 2': [69, 25, 0, 1, 44, 3, 19, 1]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_chart.xlsx', engine='xlsxwriter')
my_sheet_name = 'Totals_FY'
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name=my_sheet_name, startrow=2)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets[my_sheet_name]
# Create a chart object.
chart = workbook.add_chart({'type': 'pie'})
# Configure the series of the chart from the dataframe data.
max_row = df.shape[0] + 2
chart.add_series({
'name': [my_sheet_name, 2, 2],
'categories': [my_sheet_name, 3, 1, max_row, 1],
'values': [my_sheet_name, 4, 4, max_row, 4],
'data_labels': {
'value': True,
'category': True,
'position': 'outside_end'}
})
# Insert the chart into the worksheet.
worksheet.insert_chart('F3', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
我正在使用 python 和 xlsxwriter 在 Excel 文件中创建表格和图表。当我在图表中添加标签时,它们没有显示。
如有任何帮助,我们将不胜感激。
代码:
chart1a.add_series({
'name': '=Totals_FY!$C',
'categories': '=Totals_FY!$B:$B$' + str(types_pivot.shape[0]+3),
'values': '=Totals_FY!$D:$D$' + str(types_pivot.shape[0]+3),
'data_labels': {
'value':True,
'category_name':True,
'position':'outside_end'
})
图表 1a
Type Year 1 Year 2
a 150 69
b 48 25
c 9 0
d 0 1
e 67 44
f 0 3
g 46 19
h 0 1
data_labels
参数中有错字 ('category_name' -> 'category')。除此之外,它应该按预期工作。
这是一个基于您的示例:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Type': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
'Year 1': [150, 48, 9, 0, 67, 0, 46, 0],
'Year 2': [69, 25, 0, 1, 44, 3, 19, 1]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_chart.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Totals_FY', startrow=2)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Totals_FY']
# Create a chart object.
chart = workbook.add_chart({'type': 'pie'})
# Configure the series of the chart from the dataframe data.
chart.add_series({
'name': '=Totals_FY!$C',
'categories': '=Totals_FY!$B:$B$' + str(df.shape[0] + 3),
'values': '=Totals_FY!$D:$D$' + str(df.shape[0] + 3),
'data_labels': {
'value': True,
'category': True,
'position': 'outside_end'}
})
# Insert the chart into the worksheet.
worksheet.insert_chart('F3', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
输出:
请注意,在此类从数据框创建图表的程序中,最好使用 add_series()
:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Type': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
'Year 1': [150, 48, 9, 0, 67, 0, 46, 0],
'Year 2': [69, 25, 0, 1, 44, 3, 19, 1]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_chart.xlsx', engine='xlsxwriter')
my_sheet_name = 'Totals_FY'
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name=my_sheet_name, startrow=2)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets[my_sheet_name]
# Create a chart object.
chart = workbook.add_chart({'type': 'pie'})
# Configure the series of the chart from the dataframe data.
max_row = df.shape[0] + 2
chart.add_series({
'name': [my_sheet_name, 2, 2],
'categories': [my_sheet_name, 3, 1, max_row, 1],
'values': [my_sheet_name, 4, 4, max_row, 4],
'data_labels': {
'value': True,
'category': True,
'position': 'outside_end'}
})
# Insert the chart into the worksheet.
worksheet.insert_chart('F3', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()