简体   繁体   中英

xy scatter plot with python-pptx not working

I am trying to make a simple python-pptx xy scatter chart with x and y series data but unsuccessful so far.

from pptx import Presentation
from pptx.util import Inches,Pt

from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import XySeriesData

prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
blank_slide_layout = prs.slide_layouts[6]

slide = prs.slides.add_slide(title_slide_layout)
slide2 = prs.slides.add_slide(blank_slide_layout)

title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"
chart_data = XySeriesData
chart_data.x_values=[0,1,2,3,4,5]
chart_data.y_values=[10,22,33,38,40,43]
x, y, cx, cy = Inches(1), Inches(2), Inches(8), Inches(3)
chart = slide2.shapes.add_chart(XL_CHART_TYPE.XY_SCATTER, x, y, cx, cy, chart_data).chart

prs.save('test1.pptx')

The error i get is

File "C:\Users\adnan\Google Drive\Learning\Python\5g_tti_parser\untitled0.py", line 31, in chart = slide2.shapes.add_chart(XL_CHART_TYPE.XY_SCATTER, x, y, cx, cy, chart_data).chart

File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\shapes\shapetree.py", line 250, in add_chart rId = self.part.add_chart_part(chart_type, chart_data)

File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\parts\slide.py", line 174, in add_chart_part chart_part = ChartPart.new(chart_type, chart_data, self.package)

File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\parts\chart.py", line 30, in new chart_blob = chart_data.xml_bytes(chart_type)

AttributeError: type object 'XySeriesData' has no attribute 'xml_bytes'

I am working on this as well today - it looks like you skipped some steps.

Make sure you have the right imports, which include:

from pptx.chart.data import XySeriesData, XyChartData

Change this line:

chart_data = XySeriesData

to instantiate the class XyChartData:

chart_data = XyChartData()

then you need to add a series (which creates the XySeries object), give it a name and a number format (I am using None)

chart_data.add_series('name_of_series', number_format= None)

THEN

chart_data.x_values=[0,1,2,3,4,5]
chart_data.y_values=[10,22,33,38,40,43]

I'm not fully sure if this will solve your problem, but I hope it helps. I was able to replace the data in a scatterplot this way today.

EDIT:

Setting the x_values and y_values on the chart_data object has been inconsistent and unreliable. In example below, set your numeric lists to variable names. I am using x_values_list and y_values_list for clarity.

New method:

Save your add_series object to a variable, and add x & y data points individually.

chart_data = XyChartData()    
cd = chart_data.add_series('name_of_series', number_format= None)
    for x, y in list(zip(x_values_list, y_values_list)):
         cd.add_data_point(x, y, number_format=None)

At this point, chart_data contains your series object and can be inserted into the chart shape, while cd represents the XySeries object. I think x_values and y_values are meant as accessors more than writers, perhaps? I could not set those values at the XySeries level successfully. I had many issues trying to set my lists to those attributes at the XyChartData level as well. This is functioning much better.

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