Reputation: 1293
I am creating a few charts using python-pptx module. I am able to create the charts as required. However I am not sure how to remove the gridlines on the background of the chart.
Given below is a sample code to create bar charts:
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
# define chart data ---------------------
chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
# add chart to slide --------------------
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
)
Could anyone assist, thanks..
Upvotes: 1
Views: 1777
Reputation: 1
# Creating a bar chart
chart_data = ChartData()
.
.
.
# Creating a chart object
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
).chart
.
.
.
# Gridlines
chart.category_axis.has_major_gridlines = False #Remove Vertical Major Gridlines
chart.value_axis.has_major_gridlines = False #Remove Horizontal major Gridlines
Upvotes: 0
Reputation: 28893
Grid lines are an attribute of a chart axis, so most charts have two sets and they can be controlled separately.
The documentation has examples here: https://python-pptx.readthedocs.io/en/latest/api/chart.html#axis-objects
Upvotes: 1