Python 的 XlsxWriter 将文本框插入图表

标签 python excel charts textbox xlsxwriter

我已经创建了一个包含柱形图和折线图的组合图表。棘手的部分是在图表中插入一个文本框: 1. X轴标题下方 2. 下面的图例。

我阅读了文档并找到了 worksheet.insert_textbox()。但是,我需要一些图表(如果我正确理解文档,上述方法适用于工作表,而不是图表)。

我在 Windows Vista 64 位上使用 Anaconda Python (2.3.0)。

result = pd.ExcelWriter(r'C:/tmp/dev_excel.xlsx', engine='xlsxwriter')

# inserting image with 'xlsxwriter'
workbook = result.book

worksheet = workbook.add_worksheet()
# Add the worksheet data to be plotted.
data = ['Aaaa', 'A2', 'A3', 'A4', 20, 10, 50]
worksheet.write_column('A1', data)

data = ['Bbbbb', 10, 40, 50, 20, 10, 50]
worksheet.write_column('B1', data)

data = ['Cccc', 110, 140, 150, 120, 110, 150]
worksheet.write_column('C1', data)

# Create a new column chart. This will use this as the primary chart.
column_chart = workbook.add_chart({'type': 'column'})

# Configure the data series for the primary chart.
column_chart.add_series({
    'name':       '=Sheet1!B1',
    'categories': '=Sheet1!A2:A7',
    'values':     '=Sheet1!B2:B7',
})

# Create a new column chart. This will use this as the secondary chart.
line_chart = workbook.add_chart({'type': 'line'})

# Configure the data series for the secondary chart.
line_chart.add_series({
    'name':       '=Sheet1!C1',
    'categories': '=Sheet1!A2:A7',
    'values':     '=Sheet1!C2:C7',
    'y2_axis':    True,
})

# Add a chart title and some axis labels.
# ...
column_chart.set_y2_axis({'name': 'Target length (mm)'})

# Combine the charts.
column_chart.combine(line_chart)

# Add a chart title and some axis labels. Note, this is done via the
# primary chart.
column_chart.set_title({ 'name': 'Combined chart - same Y axis'})
column_chart.set_x_axis({'name': 'Test number'})
column_chart.set_y_axis({'name': 'Sample length (mm)'})

# Insert the chart into the worksheet
worksheet.insert_chart('E2', column_chart)

workbook.close()

result.save()

最佳答案

if I understand documentation correctly, the above method works on a sheet, not on a chart

没错。 XlsxWriter 不支持图表中的文本框。

您可以将工作表文本框覆盖在图表上,但它们将是两个独立的对象,并且将彼此独立移动。

关于Python 的 XlsxWriter 将文本框插入图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990166/

相关文章:

python - 如何使 Python Tkinter 文本在按钮和标签中自动调整大小?

python - SQLite、python、unicode 和非 utf 数据

c# - 在 ASP.NET(Microsoft 的图表控件)中自定义图表 : how to get x-axis labelling?

android - 谷歌图表水平滚动

ios - 如何使用 IOSChart 通过 ChartValueSelected 显示另一个 ViewController?

python - 什么是 pipenv [dev-packages] 部分?

python - LSTM 奇偶校验生成器

java - 如何将 Excel 电子表格配置为 javax.sql.DataSource?

php - 可以导入 .xls 文件但不能将 .xlsx 导入 MYSQL 数据库

Excel Vba。将调整大小的图像保存到文件