简体   繁体   中英

How to set data labels for XY Plot using python pptx

I'm trying to set the labels for the points in a XY Chart (Scatter Plot) using python-pptx. My code is as below:

df = pd.DataFrame({
            'x' : np.random.random(20)*100,
            'y' : np.random.random(20)
})

x = df.x.to_list()
y = df.y.to_list()


def update_xychart(chart, df):
    chart_data = XyChartData()
    plot = chart.plots[0]
    series_1 = chart_data.add_series('Model 1')
    for i in range(0,len(x)):
        series_1.add_data_point(x[i],y[i])
    chart.replace_data(chart_data)
    for index in range(0, 20):
        plot.XyPoints[index].data_label.text_frame.text = 'Label' + str(index)

I keep getting the error AttributeError: 'XyPlot' object has no attribute 'XyPoints' . I also tried plot.points[index].data_label.text_frame.text = 'Label' + str(index) but didn't work. What is the correct way to access and set the labels for the xy (scatter) chart? Also, can I set the color of every individual marker ie the dot/triangle/cross used to represent xy value on the scatter plot?

I had a similar issue, in your code you are missing the points object. Here it is with my example.

for shapes in slide:
  if shapes.has_chart:
      if shapes.chart.chart_title.text_frame.text=='Chart':
        for index in range (0,8):      
shapes.chart.plots[0].series[0].points[index].data_label.text_frame.text='Label' + str(index)

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