简体   繁体   English

openpyxl 图表上的轴文本方向

[英]Axis text orientation on openpyxl chart

I'm generating a ScatterChart with pyopenxl from a pandas dataframe.我正在使用 pyopenxl 从 pandas dataframe 生成散点图。

I am trying to change the rotation of the text in the X axis to 270º but I cannot found documentation about how to do it.我试图将 X 轴上的文本旋转更改为 270º,但我找不到有关如何执行此操作的文档。

This is the code to generate the chart.这是生成图表的代码。

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()

This is the output chart that I got.这是我得到的 output 图表。

在此处输入图像描述

This is the output chart that I want.这是我要的output图表。

在此处输入图像描述

Thanks!谢谢!

This is unfortunately nothing like as simple as it should be because in the specification this is one of the areas where the schema changes from SpreadsheetDrawingML to DrawingML, which is far more abstract. 遗憾的是,这并不像应该这么简单,因为在规范中,这是架构从SpreadsheetDrawingML更改为DrawingML的区域之一,这更加抽象。 The best thing to do is actually create two sample files and compare them. 最好的办法是创建两个示例文件并进行比较。 In this case this difference is in rot or rotation attribute of the txPr or textProperties of the axis. 在这种情况下,这个差异在于txPr rotrotation属性或轴的textProperties This is covered in § 21.1.2.1.1 of the OOXML specification. 这在OOXML规范的第21.1.2.1.1节中有所涉及。

The following code should work, but might require you to create a TextProperties object: 以下代码应该可以使用,但可能需要您创建TextProperties对象:

chart.x_axis.textProperties.bodyProperties.rot = -5400000

I had the same question - this SO post by @oldhumble solved it for me - please see Rotate the axis of an excel chart using openpyxl我有同样的问题 - @oldhumble 的这篇 SO 帖子为我解决了它 - 请参阅使用 openpyxl 旋转 excel 图表的轴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM