如何旋转移动坐标轴标题
我正在使用一个叫做 openpyxl
的库来做一些图形处理。
我需要把它水平旋转,并且移动到坐标轴的顶部。
下面是我为坐标轴制作标题的方式。
chart.y_axis.title = "g²/Гц" if sheet.cell(row=2, column=columnOffset + col_index).value == "Sxx" else "g"
1 个回答
0
你可以通过手动布局的命令来调整图例的位置。可能需要试着调整一下 x、y 和 h 这些设置,它们的值可以在 0 到 1 之间。
下面是一个使用柱状图的示例代码
import openpyxl
from openpyxl.chart.layout import Layout, ManualLayout
from openpyxl.chart import BarChart, Reference
# to create a new blank Workbook object
wb = openpyxl.Workbook()
# Get workbook active sheet from the active attribute.
sheet = wb.active
# write 2 to 12 in 1st column of the active sheet
for i in range(2, 13):
sheet.append([i])
# Change the Y axis name
sheet['C2'].value = 'Sxx'
# Set the co-ords for the postion
columnOffset = 2
col_index = 1
# create data for plotting
values = Reference(sheet, min_col=1, min_row=1, max_col=1, max_row=10)
# Create object of BarChart class
chart = BarChart()
# adding data to the Bar chart object
chart.add_data(values)
# set the title of the chart
chart.title = " BAR-CHART "
# set the title of the x-axis
chart.x_axis.title = " X_AXIS "
# set the title of the y-axis
chart.y_axis.title = "g²/Гц" if sheet.cell(row=2, column=columnOffset + col_index).value == "Sxx" else "g"
# Set chart Y axis text to horizontal orientation
chart.y_axis.txPr = chart.y_axis.title.text.rich
chart.y_axis.txPr.properties.vert = "horz"
# Move height of Y axis legend
chart.y_axis.title.layout = Layout(
manualLayout=ManualLayout(
h=0.85, # value between 0 and 1
x=0,
y=0.9
)
)
# Add Sheet chart
sheet.add_chart(chart, "E2")
# save the file
wb.save("barChart1.xlsx")