简体   繁体   中英

python-pptx: Set the value axis of a chart to cross at 0

I'd like to create a chart using python-pptx where the value axis always starts at 0. It appears from reading the docs that this should be possible using the crosses and crosses_at setters. However, when I try this using the code below, the value axis still starts at 314. Am I misunderstanding the api here or is this a bug?

from pptx import Presentation
from pptx.enum.chart import XL_AXIS_CROSSES
from pptx.chart.data import CategoryChartData
from pptx.util import Inches
from pptx.enum.chart import XL_CHART_TYPE


prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (319.2, 321.4, 316.7))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
graphic_frame = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)
chart = graphic_frame.chart

value_axis = chart.value_axis
value_axis.crosses = XL_AXIS_CROSSES.CUSTOM
value_axis.crosses_at = 0  # always start value axis at 0

prs.save('demo.pptx')

I believe what you're looking for is value_axis.minimum_scale = 0 .

By default, this value is not specified (returns None ), which means "auto". That setting will adjust the minimum value of the displayed range to make best use of the display area (according to some algorithm).

When you specify it explicitly, that end of the display-range becomes fixed.

The category-axis crossing at the minimum displayed value is the default behavior, so you should not need the .crosses or .crosses_at settings in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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