Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.dataimportXySeriesData, 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 inlist(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.

Post a Comment for "Xy Scatter Plot With Python-pptx Not Working"