python - 在 pandas 中的图表下插入描述

标签 python pandas graph charts

我使用这个示例来开始使用 pandas:

http://pandas-xlsxwriter-charts.readthedocs.io/chart_grouped_column.html#chart-grouped-column

我也想将图表保存在 Excel 中,就像示例中一样

我想知道如何在上面的示例中,我可以在图表下添加描述或表格

我发现的唯一相关的是: Add graph description under graph in pylab

但是这是用 pylab 完成的,用 pandas 和 Excel 图表也可以吗?

最佳答案

在 Excel 中,您可以添加文本框并插入一些文本,但 XlsxWriter 无法做到这一点。

您可以使用图表 title 属性,但在 Excel 中,标题通常位于顶部而不是底部。

您可以在 Excel 中手动重新定位它。这也可以通过使用不同图表对象的 layout 选项的 XlsxWriter 实现。

这是一个例子:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()

# Create a new Chart object.
chart = workbook.add_chart({'type': 'column'})

# 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$1:$A$5'})
chart.add_series({'values': '=Sheet1!$B$1:$B$5'})
chart.add_series({'values': '=Sheet1!$C$1:$C$5'})

chart.set_x_axis({'name': 'X axis title'})
chart.set_y_axis({'name': 'Y axis title'})

chart.set_title({
    'name': 'Here is some text to describe the chart',
    'name_font':  {'bold': False, 'size': 10},
    'layout': {
        'x': 0.25,
        'y': 0.90,
    }
})

chart.set_plotarea({
    'layout': {
        'x':      0.11,
        'y':      0.10,
        'width':  0.75,
        'height': 0.60,    
    }
})

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

workbook.close()

请注意,您需要对 layout 进行一些尝试和错误。属性来获取您想要的布局。

输出:

enter image description here

关于python - 在 pandas 中的图表下插入描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49791279/

相关文章:

python - 任务 Celery 中的 RuntimeError : Never call result. get()

python - 如何将带有列表值的 pandas 列连接到一个列表中?

python - PANDAS 中的累积集

python - 加速从 292 万个数据点创建图表

python - 如何在 django_filters 中使用 ModelMultipleChoiceFilter 和 JSONfield

python - 在 python 中为 **https** 连接指定具有用户名和密码的代理的最佳方法是什么?

Python Dataframe 条件和

javascript - 在Sigma.js中加载JSON文件但无法绑定(bind)overEdge事件

ruby - 获取具有加权顶点和边的图中的路径

python - 为什么同一 Gurobi LP 型号的双重价格不同?