openpyxl图表上的轴文本方向

时间:2017-05-06 00:58:58

标签: python excel openpyxl

我正在从pandas数据帧生成带有pyopenxl的ScatterChart。

我正在尝试将X轴上的文字旋转更改为270º,但我找不到有关如何操作的文档。

这是生成图表的代码。

import numpy as np
from openpyxl.chart import ScatterChart, Reference, Series
from openpyxl.chart.axis import DateAxis
import pandas as pd

def generate_chart_proyeccion(writer_sheet, col_to_graph, start_row, end_row, title):
    """
    Construct a new chart object

    :param writer_sheet: Worksheet were is data located
    :param col_to_graph: Column of data to be plotted
    :param start_row: Row where data starts
    :param end_row: Row where data ends
    :param title: Chart title
    :return: returns a chart object
    """
    chart = ScatterChart()
    chart.title = title
    chart.x_axis.number_format = 'd-mmm HH:MM'
    chart.x_axis.majorTimeUnit = "days"
    chart.x_axis.title = "Date"
    chart.y_axis.title = "Value"
    chart.legend.position = "b"
    data = Reference(writer_sheet, min_col=col_to_graph, max_col=col_to_graph, min_row=start_row, max_row=end_row)
    data_dates = Reference(writer_sheet, min_col=1, max_col=1, min_row=start_row, max_row=end_row) # Corresponde a la columna con la fecha
    serie = Series(data, data_dates, title_from_data=True)
    chart.series.append(serie)
    return chart

# Write data to excel
writer = pd.ExcelWriter("file.xlsx", engine='openpyxl')
df = pd.DataFrame(np.random.randn(10,1), columns=['Value'], index=pd.date_range('20130101',periods=10,freq='T'))
start_row = 1 # Row to start writing df in excel
df.to_excel(writer, sheet_name="Sheet1", startrow=start_row) 
end_row = start_row + len(df) # End of the data
chart = generate_chart_proyeccion(writer.sheets["Sheet1"], 2, start_row, end_row, "A title")
# Añado gráfico a excel
writer.sheets["Sheet1"].add_chart(chart, "C2")  
writer.save()

这是我得到的输出图表。

enter image description here

这是我想要的输出图表。

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:0)

遗憾的是,这并不像应该的那样简单,因为在规范中,这是架构从SpreadsheetDrawingML更改为DrawingML的区域之一,这更加抽象。最好的办法是创建两个示例文件并进行比较。在这种情况下,此差异位于轴的 <div class="content container_12"> <ul id="list-content"> <li> <img src="https://img1.doubanio.com/lpic/s27653128.jpg" alt="inferno cape"> <div class="desc"> <h2 class="header-content text-content">This is a heading</h2> <time datetime="2017-01-01" class="text-content">Game Updates | January 1st, 2017</time> <p class="paragraph-content text-content">This is the content</p> </div> </li> <li> <img src="https://img3.doubanio.com/lpic/s4554820.jpg" alt="find option"> <div class="desc"> <h2 class="header-content text-content">This is a heading</h2> <time datetime="2017-01-01" class="text-content">Game Updates | January 1st, 2017</time> <p class="paragraph-content text-content">This is the content</p> </div> </li> <li> <img src="https://img3.doubanio.com/lpic/s8958650.jpg" alt="duel arena"> <div class="desc"> <h2 class="header-content text-content">This is a heading</h2> <time datetime="2017-01-01" class="text-content">Game Updates | January 1st, 2017</time> <p class="paragraph-content text-content">This is the content</p> </div> </li> </ul> </div>rot的{​​{1}}或rotation属性中。这在OOXML规范的第21.1.2.1.1节中有所涉及。

以下代码应该有效,但可能需要您创建txPr对象:

textProperties