[英]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.我正在尝试使用 x 和 y 系列数据制作一个简单的 python-pptx xy 散点图,但到目前为止没有成功。
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文件“C:\Users\adnan\Google Drive\Learning\Python\5g_tti_parser\untitled0.py”,第 31 行,在图表 = slide2.shapes.add_chart(XL_CHART_TYPE.XY_SCATTER, x, y, cx, cy, chart_data) 中。图表
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)文件“C:\ProgramData\Anaconda3\lib\site-packages\pptx\shapes\shapetree.py”,第 250 行,在 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)文件“C:\ProgramData\Anaconda3\lib\site-packages\pptx\parts\slide.py”,第 174 行,在 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)文件“C:\ProgramData\Anaconda3\lib\site-packages\pptx\parts\chart.py”,第 30 行,在新的 chart_blob = chart_data.xml_bytes(chart_type)
AttributeError: type object 'XySeriesData' has no attribute 'xml_bytes' AttributeError:类型 object 'XySeriesData' 没有属性 '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:实例化 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)那么你需要添加一个系列(创建 XySeries 对象),给它一个名称和一个数字格式(我使用的是无)
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.在 chart_data object 上设置 x_values 和 y_values 不一致且不可靠。 In example below, set your numeric lists to variable names.
在下面的示例中,将您的数字列表设置为变量名称。 I am using x_values_list and y_values_list for clarity.
为了清楚起见,我使用 x_values_list 和 y_values_list。
New method:新方法:
Save your add_series object to a variable, and add x & y data points individually.将您的 add_series object 保存到变量中,并分别添加 x 和 y 数据点。
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.此时,chart_data 包含您的系列 object 并且可以插入图表形状,而 cd 代表 XySeries object。 I think x_values and y_values are meant as accessors more than writers, perhaps?
我认为 x_values 和 y_values 的意思是作为访问者而不是作家,也许? I could not set those values at the XySeries level successfully.
我无法在 XySeries 级别成功设置这些值。 I had many issues trying to set my lists to those attributes at the XyChartData level as well.
我在尝试将列表设置为 XyChartData 级别的这些属性时也遇到了很多问题。 This is functioning much better.
这运行得更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.