orange 数据分析_使用Orange GUI的放置结果数据分析

本文分析了影响学生招聘的各种因素,包括中学和大学成绩、性别、学术专业和工作经验等,并使用橙色数据分析工具进行可视化。研究发现,男性学生、商业和管理专业的学生以及在高中和大学取得较高分数的学生在招聘中更占优势。性别、教育背景、工作经历和学术成绩与薪资水平直接相关。此外,工作经验和就业能力测试成绩也是重要因素。分类树模型揭示了不同学术和非学术因素如何影响招聘和薪资待遇。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

orange 数据分析

Objective : Analysing of several factors influencing the recruitment of students and extracting information through plots.

目的: 分析影响学生招生和通过情节提取信息的几个因素。

Description : The following analysis presents the different plots that attempts to link students’ placement prospects, made possible through student perceptions of recruiting organisations to certain academic parameters such as percentage obtained in secondary and higher secondary school, undergraduate degree and post graduation degree.

Description(说明) :以下分析提出了不同的图,这些图试图通过将学生对招募组织的理解与某些学术参数(例如,在中学和高中获得的百分比,大学学位和毕业学位)的理解联系起来,从而尝试联系学生的就业前景。

Miscellaneous factors such as the gender of the candidate, the choice of board for and the stream opted for in high school and secondary education, undergraduate degree specialisation and post graduate degree specialisation have also been taken into account to predict placement status as well as salary offered.

还考虑了其​​他因素,例如候选人的性别,高中和中等教育的董事会选择和选择的职位,本科学位专业和研究生学位专业,以预测安置状况以及所提供的薪水。

Several colleges offer employ-ability tests which serve as a way of helping the employers evaluate their workforce, analyse and judge their skills and hence recruit the right talent. Thus, performance of students in such tests conducted by the college and their previous work experience have also been analysed to deduce their relation with recruitment opportunities.

几所大学提供就业能力测试,以帮助雇主评估其劳动力,分析和判断其技能,从而招募合适的人才。 因此,还对学生在大学进行的此类测试中的表现以及他们以前的工作经验进行了分析,以推断出他们与招聘机会的关系。

Hypothesis : Students with better scores in secondary education and undergraduate degree have better prospects of getting placed.

假设 :中学教育和大学学位较高的学生有更好的入学前景。

Understanding the Project :

了解项目

Going through the analysis, a reader shall be able to infer :

通过分析,读者应能够推断:

  1. How the choice of board of education influences placement prospects.

教育委员会的选择如何影响安置前景。

  1. The relative importance of scores obtained in various degrees and streams in campus recruitment procedure.

在校园招聘过程中,不同程度和不同等级获得的分数的相对重要性。

  1. The relation between gender and work experience with salary offered by corporate on campus placements.

性别和工作经验与公司在校园安置中提供的薪水之间的关系。

Acknowledgements:

致谢:

Myself Ruchika Parag Barman and my team mate Prafful Chauhan created this notebook/blog as part of the course work under “Pandas, bamboolib & Orange workshop” at Suven , under mentor-ship of Rocky Jagtiani .

我自己的 Ruchika Parag Barman 和我的队友 Prafful Chauhan Rocky Jagtiani的 指导下,在 Suven的 “熊猫,竹筒和橙子工作坊” 下创建了此笔记本/博客,作为该课程工作的一部分。

Learned from https://datascience.suvenconsultants.com .

https://datascience.suvenconsultants.com 了解到。

Mentored by Rocky Jagtiani .

Rocky Jagtiani指导

Dataset:

资料集:

This data set consists of Placement data of students in a XYZ campus. It includes secondary and higher secondary school percentage and specialization. It also includes degree specialization, type and Work experience and salary offers to the placed students.

此数据集包含XYZ校园中学生的安置数据。 它包括中学和高中的百分比和专业。 它还包括学位专业化,类型和工作经验以及向所安置学生提供的薪水。

Image for post

We have taken 60 observations (no of rows) from which we are extract information through exploratory data analysis and visualization. There are 8 categorical features and 6 numerical features.

我们采用了60个观测值(无行),通过探索性数据分析和可视化从中提取信息。 有8个分类特征和6个数字特征。

Histograms :

直方图:

Image for post

Inference : Male students are getting more placements than female students and the ratio of male to female in placements is almost around 2:1.

推论 学生变得比 女生 更多 的展示位置男性 的比例, 女性 配股 几乎是2:1左右。

Image for post

Inference : We can inspect that with respect to high school education, Central board students have wider range of salary than the other board students but placement ratio central to others is less than 1.

推论 :我们可以检查到,就高中教育而言, 中央董事会 学生的 薪资 范围比 其他董事会 学生要大,但相对于 其他人而言, 中心 职位的就业 率低于1。

Image for post

Inference : We can inspect that with respect to secondary education, Central board students have wider range of salary than the other board students.

推论 :我们可以检查到,就中等教育而言, 中央董事会 学生的 薪资 范围比 其他董事会 学生要广。

Image for post

Inference : Commerce and Arts students have wider range of salary and number of placed students are more as compared to science or other stream .

推论 :与 理科其他 专业 相比, 商科文科 生的 薪资 范围更广,安置学生的数量也更多。

From the above graphs, one can gather that gender plays quite an important role in whether or not a candidate will be hired. It is more likely for a male candidate to get placed at a corporate as compared to a female candidate. Similarly, the board of education and the stream chosen also determine salary offered. Students have been proposed higher amounts of pay that opted for Commerce and Management studies.

从以上图表可以看出,性别在是否应聘者中起着非常重要的作用。 与女性候选人相比,男性候选人更有可能被安置在公司。 同样,教育委员会和所选择的职位也决定了提供的薪水。 建议学生选择更高的薪水,选择商务和管理学习。

Correlations :

相关性

Image for post

The correlations table gives us the following ideas :

相关表为我们提供了以下想法:

  1. Students who have scored well in their secondary education are very likely to perform well in their undergraduate degree also.

中学教育 中取得良好成绩的学生,其 本科学位 也很可能会表现良好。

  1. Students who have scored well in their high school education eventually perform well in their secondary education also.

高中阶段 成绩良好的 学生 最终在 中等教育方面 也表现良好。

  1. Again, students who have scored well in their high school education are very likely to perform well in their undergraduate degree also.

同样,在 高中阶段 取得良好 成绩的学生 也很可能在 本科学位 上表现良好。

  1. Most students who have had a good academic record in their high school education also score high in their MBA degree .

大多数 高中学历 良好的学生的 MBA学位 也很高。

Boxplots :

箱线图

Image for post

Inference : The above boxplot shows the relation between percentage obtained in the undergraduate degree and placement status . Students who get placed score higher than those who do not get placed. The mean score of placed students is given by 68.6925, standard deviation is 6.189 ,2nd quartile or median is 69.25 ,1st quartile is 64.50 and 3rd quartile is 72.1150.

推论 :上面的方框图显示 了本科学位所占百分比升学状况的关系 。 被安置的学生的得分高于没有被安置的学生。 留学生的平均分数为68.6925,标准差为6.189,第二四分位数或中位数为69.25,第一四分位数为64.50,第三四分位数为72.1150。

Whereas, the mean percentage of students not placed is given by 60.8670, standard deviation is 7.045, 2nd quartile or median is 61.00, 1st quartile is 56.65 and 3rd quartile is 64.00.

而未安置学生的平均百分比为60.8670,标准差为7.045,第二四分位数或中位数是61.00,第一四分位数是56.65,第三四分位数是64.00。

From this analysis, undergraduate students/freshers can prioritise and prepare for their undergraduate/degree examinations keeping in mind the average score, as mentioned above, that the corporate companies generally perceive worthy of grabbing a placement in their establishment.

通过这种分析,本科生/新生可以优先考虑并为本科生/学位考试做准备,同时牢记如上所述的平均分数,即公司通常认为值得在其机构中获得职位。

Image for post

Inference : Male candidates get a higher pay than female candidates . The mean salary of placed male students is given by 302608.70 , standard deviation is 144726.4 , 2nd quartile or median is 264000, 1st quartile is 240000 and 3rd quartile is 300000.

推论男性候选人 的薪酬高于 女性候选人 。 入学男生的平均工资为302608.70,标准差为144726.4,第二四分位数或中位数为264000,第一四分位数为240000,第三四分位数为300000。

On the other hand, the mean salary of placed female students is given by 267571.43, standard deviation is 41776.1, 2nd quartile or median is 250000 ,1st quartile is 240000 and 3rd quartile is 300000.

另一方面,入职女学生的平均工资为267571.43,标准差为41776.1,第二四分位数或中位数为250000,第一四分位数为240000,第三四分位数为300000。

Thus, we can see that while the placement rate of females is lower than males, the salary offered to the placed female candidates is also relatively lower than that of the male candidates.

因此,我们可以看到,尽管女性的就业率低于男性,但提供给被安置的女性候选人的薪水也相对低于男性候选人。

Pivot Table :

数据透视表

Image for post

Inference : As more students opt for Commerce and Management , the no. of placed students as well as students not placed are much higher in it as compared to Science and other streams . Even the ratio of placed to students not placed is higher in Commerce and Management is higher than that in Science.

推论 :随着越来越多的学生选择 商业与管理 ,不 与 理科和其他科目 相比, 录取 学生和未录取学生的比例要高得多。 即使在商务和管理领域,就读率和未就读率之间的比重也更高,而在理科中则更高。

Readers can understand there are relatively more job opportunities for students who opt for Commerce and Management than other streams.

读者可以理解,选择商业和管理专业的学生比其他领域的工作机会相对更多。

Scatterplots :

散点图

Image for post

For scatterplots, we have used 60% of the data provided. A scatterplot with variables salary and percentage obtained in the degree examination is formed. Here,the different points have been coloured according to the different streams as shown in the legends table.

对于散点图,我们使用了提供的60%的数据。 形成 了在学位考试中获得的 薪水百分比 可变的散点图。 在这里,不同的点已根据图例表中所示的 不同流 进行了着色。

Inference : The higher salaries have been offered to students whose scores lie in the range 64–74. Moreover, from the point of stream , most of the students that have been offered a pay higher than 300,000 belong to Commerce and Management. Very few students of Science and even fewer students of other streams have crossed the threshold of 300,000 pay.

推论 :为 分数 在64-74之间的学生提供了更高的 薪水 。 而且,从 的角度来看,获得超过30万薪水的大多数学生属于商业与管理专业。 理科专业的学生很少,其他流派的学生甚至超过了30万。

Image for post

Inference : Students that specialise in Marketing and Finance and those in Marketing and HR score similarly in MBA percentage . However, the highest paid students generally have scores in the range 62–70, approximately. Very few students have been offered a pay higher than 400,000. Majority of students are offered salaries in the range of 250,000 to 350,000.

推论市场营销与金融 专业的学生, 市场 营销与人力资源 专业的MBA百分比 得分相似。 但是,收入最高的学生的分数通常在62-70之间。 很少有学生获得高于40万的薪水。 大多数学生的 薪水 在250,000到350,000之间。

We can understand that maintaining an average score that falls in the above mentioned range shall suffice for a decent paying placement.

我们可以理解,将平均得分保持在上述范围内就足以获得不错的付费。

Mosaic Plot :

马赛克图

Image for post

Other than academic parameters, some other factors may also be considered for placement by recruiting companies. Employablity tests conducted by colleges are key for establishing appropriate labour market linkages and ascertaining that the workforce is industry ready.

除了学术参数,其他一些因素也可以由招聘单位考虑 的位置 。 高校进行的 能力测试 对于建立适当的劳动力市场联系并确定劳动力已做好行业准备至关重要。

Inference : From the plot above, we can see that of all the students that did not get placed, very few scored above 83.5. Most of the unemployed candidates scored below 83.5.

推论 :从上图可以看出,在所有未获得排名的学生中,只有极少数得分高于83.5。 大多数失业候选人的得分都低于83.5。

Moreover, the plot suggests that students having prior work experience are considered more deserving than freshers. Nearly all the sections of students not placed did not have a prior work experience, whereas those having work experience are on the placed students section on the right.

此外,该图表明,具有过往 工作经验的 学生被认为比新生更值得。 几乎所有未安置学生的部分都没有事先的工作经验,而那些有工作经验的学生则在右侧的已安置学生部分。

From this, students can comprehend that having an experience in a work environment before campus recruitment proves to be beneficial. Thus, they can plan and prepare accordingly for their future.

由此,学生可以理解,在校园招聘之前的工作环境中的经验被证明是有益的。 因此,他们可以为自己的未来做计划并作相应的准备。

Classification Tree :

分类树

Image for post

This classification tree has placement status (placed) as target .It has the following parameters:

该分类树以 放置状态(已放置) 为目标,具有以下参数:

It is an induced binary tree.

它是一个诱导二叉树。

Minimum no. of instances in leaves : 2.

最低编号 叶子中的实例数量:2。

Do not split subsets more than :5.

子集分割不要超过:5。

Limit the maximal tree depth to : 100.

将最大树深度限制为:100。

Classification stops when majority reaches 95%.

当多数达到95%时,分类将停止。

Students can acquire a detailed analysis about the dependence of the various academic and other factors on whether or not a candidate gets placed based on the data provided. This tree gives a clear explanation of how the different attributes of a particular student shall influence their placement status .

学生可以根据所提供的数据,详细了解 各种学术因素和其他因素 对候选人是否被安置的依赖性。 该树清楚地解释了特定学生的不同属性如何影响他们的 位置状况

Image for post

This classification tree has salary offered as target .It has the following parameters:

此分类树以 薪金 为目标,它具有以下参数:

It is an induced binary tree.

它是一个诱导二叉树。

Minimum no. of instances in leaves : 2.

最低编号 叶子中的实例数量:2。

Do not split subsets more than :5.

子集分割不要超过:5。

Limit the maximal tree depth to : 100.

将最大树深度限制为:100。

Classification stops when majority reaches 95%.

当多数达到95%时,分类将停止。

Students can acquire a detailed analysis about the dependence of the various academic and other factors on the salary offered to a candidate. This tree gives a clear explanation of how the different attributes of a particular student shall influence their pay.

学生可以获得有关各种 学术和其他因素 对应聘者 薪水 的依赖性的详细分析。 这棵树清楚地说明了特定学生的不同属性将如何影响他们的工资。

Vote of Thanks :

感谢票:

I would like to humbly and sincerely thank my mentor Rocky Jagtiani . He is more of a friend to me than mentor .The data analytics taught by him and various assignments we did and are still doing is the best way to learn and skill in Data Science field.

我要衷心地感谢我的导师 洛基 对于我而言,他不是导师,而是导师。他教给我们的数据分析以及我们目前做的和仍在做的各种作业是在数据科学领域学习和技能的最佳方法。

Recommended https://datascience.suvenconsultants.com/

推荐的 https://datascience.suvenconsultants.com/

翻译自: https://medium.com/@ruchikaparag18/placement-outcomes-data-analysis- using-orange-gui-1884aa3ac0c2

orange 数据分析

<think>问题分析: 用户在使用PyQt绘制饼图时,试图从中心点到扇区边缘绘制线条,但在访问`labelPosition()`返回的`QPointF`对象时出现了错误:'LabelPosition' object has no attribute 'x'。 这通常是因为`labelPosition()`返回的是一个枚举值(表示标签位置),而不是一个坐标点。正确的做法应该是使用`slice->labelPosition()`来获取标签位置枚举,然后使用`slice->labelGeometry()`来获取标签的几何信息(包含位置),但注意:`labelGeometry()`返回的是标签的矩形(QRectF),而不是边缘点。 实际上,在PyQt中,要获取标签的位置,我们需要使用`slice->labelGeometry().center()`或者根据饼图扇区边缘的几何信息来计算边缘点。 但是,更直接的方法是使用`slice->pie()->pos()`和扇区的几何信息来计算扇区边缘的中点。然而,在PyQt中,我们可以通过扇区的起始角度和跨度角度来计算边缘点。 因此,有两种方法可以解决: 方法1:通过计算扇区边缘中点(使用角度计算) 方法2:通过标签的几何位置(但标签位置可能并不在边缘中点上,所以方法1更精确) 在之前的代码中,我们使用了方法1(角度计算)和方法2(标签位置)。但用户遇到的错误可能是因为在PyQt中,`labelPosition()`返回的是枚举,而不是坐标。 所以,我们需要调整代码,避免直接使用`labelPosition()`作为坐标点。 下面提供两种PyQt实现: 方法1:使用角度计算边缘点(推荐) 方法2:使用标签的几何中心(可能不精确,但简单) 这里我们重点使用方法1,因为更精确。 我们重新实现一个PyQt的饼图,使用角度计算边缘点,并绘制从中心到边缘中点的线条。 完整代码示例:</think># 解决 'LabelPosition' object has no attribute 'x' 错误 这个错误通常发生在尝试访问 `QPieSlice.labelPosition()` 返回值的坐标属性时。在 PyQt 中,`labelPosition()` 返回的是一个枚举值(`QPieSlice.LabelPosition`),而不是坐标点。下面我将提供完整的解决方案和正确的实现方法。 ## 问题原因分析 `QPieSlice.labelPosition()` 返回的是标签位置的枚举值,不是坐标点: - `QPieSlice.LabelOutside` (0) - 标签在扇区外部 - `QPieSlice.LabelInside` (1) - 标签在扇区内部 - `QPieSlice.LabelInsideHorizontal` (2) - 标签在扇区内部水平放置 - `QPieSlice.LabelOutside` (3) - 标签在扇区外部并带连接线 要获取标签的实际坐标位置,我们需要使用 `labelGeometry()` 方法。 ## 正确解决方案 ### 方法 1: 使用 labelGeometry() 获取标签位置 ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice from PyQt5.QtCore import Qt, QPointF, QRectF from PyQt5.QtGui import QPen, QColor from PyQt5.QtWidgets import QGraphicsLineItem class CorrectPieChartView(QChartView): def __init__(self, parent=None): super().__init__(parent) self.lines = [] # 创建饼图系列 self.series = QPieSeries() self.series.append("Apple", 30) self.series.append("Banana", 20) self.series.append("Orange", 25) self.series.append("Grapes", 15) self.series.append("Mango", 10) # 设置扇区标签位置 for slice in self.series.slices(): slice.setLabelVisible(True) slice.setLabelPosition(QPieSlice.LabelOutside) # 设置标签位置枚举 slice.setLabelArmLengthFactor(0.2) # 标签臂长度 # 创建图表 self.chart = QChart() self.chart.addSeries(self.series) self.chart.setTitle("Correct Pie Chart Implementation") self.chart.legend().setVisible(True) self.chart.setAnimationOptions(QChart.SeriesAnimations) self.setChart(self.chart) # 连接信号:饼图布局更新时重绘线条 self.series.countChanged.connect(self.update_lines) self.chart.plotAreaChanged.connect(self.update_lines) # 初始更新线条 self.update_lines() def update_lines(self): """更新连接线(使用正确的标签几何位置)""" # 清理旧线条 for line in self.lines: self.scene().removeItem(line) self.lines = [] # 获取饼图中心点(场景坐标) plot_area = self.chart.plotArea() center = plot_area.center() # 为每个扇区创建线条 for slice in self.series.slices(): # 正确方法:获取标签几何矩形并计算中心点 label_rect = slice.labelGeometry() label_center = label_rect.center() # 创建线条图形项 line = QGraphicsLineItem( center.x(), center.y(), label_center.x(), label_center.y() ) # 设置线条样式 pen = QPen(QColor(70, 130, 180), 2.0, Qt.SolidLine, Qt.RoundCap) line.setPen(pen) # 添加到场景 self.scene().addItem(line) self.lines.append(line) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Correct Pie Chart Example") self.setGeometry(100, 100, 800, 600) # 创建中心部件 central_widget = QWidget() self.setCentralWidget(central_widget) # 设置布局 layout = QVBoxLayout(central_widget) # 添加饼图视图 self.chart_view = CorrectPieChartView() layout.addWidget(self.chart_view) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` ### 方法 2: 使用三角函数计算边缘点(更精确) ```python import sys import math from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice from PyQt5.QtCore import Qt, QPointF, QRectF from PyQt5.QtGui import QPen, QColor, QPainter from PyQt5.QtWidgets import QGraphicsLineItem class TrigonometricPieChartView(QChartView): def __init__(self, parent=None): super().__init__(parent) self.lines = [] # 创建饼图系列 self.series = QPieSeries() fruits = [ ("Apple", 30, QColor(255, 100, 100)), ("Banana", 20, QColor(255, 220, 100)), ("Orange", 25, QColor(255, 150, 50)), ("Grapes", 15, QColor(150, 100, 255)), ("Mango", 10, QColor(255, 200, 50)) ] for name, value, color in fruits: slice = QPieSlice(name, value) slice.setLabelVisible(True) slice.setLabelPosition(QPieSlice.LabelOutside) slice.setBrush(color) self.series.append(slice) # 创建图表 self.chart = QChart() self.chart.addSeries(self.series) self.chart.setTitle("Trigonometric Pie Chart") self.chart.legend().setVisible(True) self.chart.setAnimationOptions(QChart.SeriesAnimations) self.setChart(self.chart) # 连接信号 self.series.countChanged.connect(self.update_lines) self.chart.plotAreaChanged.connect(self.update_lines) # 初始更新线条 self.update_lines() def update_lines(self): """使用三角函数计算边缘点""" # 清理旧线条 for line in self.lines: self.scene().removeItem(line) self.lines = [] # 获取饼图中心点(场景坐标) plot_area = self.chart.plotArea() center = plot_area.center() radius = min(plot_area.width(), plot_area.height()) / 2 # 为每个扇区创建线条 for slice in self.series.slices(): # 计算扇区边缘中点(角度中点) mid_angle = slice.startAngle() + slice.angleSpan() / 2 radian = math.radians(mid_angle) # 计算边缘点坐标(注意Qt坐标系Y轴向下) edge_point = QPointF( center.x() + radius * math.cos(radian), center.y() - radius * math.sin(radian) ) # 创建线条图形项 line = QGraphicsLineItem( center.x(), center.y(), edge_point.x(), edge_point.y() ) # 设置线条样式 pen = QPen(QColor(50, 50, 50), 1.5, Qt.SolidLine, Qt.RoundCap) line.setPen(pen) # 添加到场景 self.scene().addItem(line) self.lines.append(line) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Trigonometric Pie Chart Example") self.setGeometry(100, 100, 800, 600) central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) self.chart_view = TrigonometricPieChartView() layout.addWidget(self.chart_view) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` ## 关键区别:正确获取位置的方法 | 方法 | 正确代码 | 错误代码 | 说明 | |------|----------|----------|------| | 标签位置 | `slice.labelGeometry().center()` | `slice.labelPosition()` | `labelPosition()` 返回枚举值,不是坐标 | | 扇区边缘点 | 三角函数计算 | `slice.labelPosition()` | 标签位置可能不在扇区边缘中点 | ## 完整解决方案:带交互功能的饼图 ```python import sys import random from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QComboBox) from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice from PyQt5.QtCore import Qt, QPointF, QRectF from PyQt5.QtGui import QPen, QColor, QPainter from PyQt5.QtWidgets import QGraphicsLineItem, QGraphicsEllipseItem class AdvancedPieChartView(QChartView): def __init__(self, parent=None): super().__init__(parent) self.lines = [] self.edge_points = [] self.center_point = None # 创建饼图系列 self.series = QPieSeries() fruits = [ ("Apple", 30, QColor(255, 100, 100)), ("Banana", 20, QColor(255, 220, 100)), ("Orange", 25, QColor(255, 150, 50)), ("Grapes", 15, QColor(150, 100, 255)), ("Mango", 10, QColor(255, 200, 50)) ] for name, value, color in fruits: slice = QPieSlice(name, value) slice.setLabelVisible(True) slice.setLabelPosition(QPieSlice.LabelOutside) slice.setBrush(color) slice.setLabelColor(QColor(50, 50, 50)) slice.setLabelArmLengthFactor(0.2) self.series.append(slice) # 创建图表 self.chart = QChart() self.chart.addSeries(self.series) self.chart.setTitle("Advanced Pie Chart with Connection Lines") self.chart.legend().setVisible(True) self.chart.setAnimationOptions(QChart.SeriesAnimations) self.setChart(self.chart) self.setRenderHint(QPainter.Antialiasing) # 连接信号 self.series.countChanged.connect(self.update_lines) self.chart.plotAreaChanged.connect(self.update_lines) # 初始更新线条 self.update_lines() def update_lines(self, method="trigonometric"): """更新连接线(支持多种方法)""" # 清理旧元素 for item in self.lines + self.edge_points: self.scene().removeItem(item) if self.center_point: self.scene().removeItem(self.center_point) self.lines = [] self.edge_points = [] self.center_point = None # 获取饼图中心点(场景坐标) plot_area = self.chart.plotArea() center = plot_area.center() radius = min(plot_area.width(), plot_area.height()) / 2 # 创建中心点标记 self.center_point = QGraphicsEllipseItem(center.x()-5, center.y()-5, 10, 10) self.center_point.setBrush(QColor(255, 0, 0)) self.center_point.setPen(Qt.NoPen) self.scene().addItem(self.center_point) # 为每个扇区创建连接线 for i, slice in enumerate(self.series.slices()): if method == "label_center": # 方法1:使用标签中心点 label_rect = slice.labelGeometry() edge_point = label_rect.center() elif method == "trigonometric": # 方法2:使用三角函数计算边缘点 mid_angle = slice.startAngle() + slice.angleSpan() / 2 radian = math.radians(mid_angle) edge_point = QPointF( center.x() + radius * math.cos(radian), center.y() - radius * math.sin(radian) ) else: # 默认方法 edge_point = center + QPointF(radius, 0) # 创建线条图形项 line = QGraphicsLineItem( center.x(), center.y(), edge_point.x(), edge_point.y() ) # 设置线条样式 pen = QPen(slice.brush().color().darker(150), 1.5, Qt.SolidLine, Qt.RoundCap) line.setPen(pen) self.scene().addItem(line) self.lines.append(line) # 创建边缘点标记 edge_marker = QGraphicsEllipseItem(edge_point.x()-4, edge_point.y()-4, 8, 8) edge_marker.setBrush(slice.brush().color().lighter(120)) edge_marker.setPen(Qt.NoPen) self.scene().addItem(edge_marker) self.edge_points.append(edge_marker) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Advanced Pie Chart Solution") self.setGeometry(100, 100, 900, 700) # 创建中心部件 central_widget = QWidget() self.setCentralWidget(central_widget) # 主布局 main_layout = QVBoxLayout(central_widget) # 添加饼图视图 self.chart_view = AdvancedPieChartView() main_layout.addWidget(self.chart_view, 4) # 控制面板 control_layout = QHBoxLayout() # 方法选择下拉框 control_layout.addWidget(QLabel("连接点计算方法:")) self.method_combo = QComboBox() self.method_combo.addItem("三角函数计算 (推荐)", "trigonometric") self.method_combo.addItem("标签中心位置", "label_center") self.method_combo.currentIndexChanged.connect(self.update_method) control_layout.addWidget(self.method_combo) # 随机数据按钮 self.btn_random = QPushButton("随机数据") self.btn_random.clicked.connect(self.randomize_data) control_layout.addWidget(self.btn_random) # 重置按钮 self.btn_reset = QPushButton("重置数据") self.btn_reset.clicked.connect(self.reset_data) control_layout.addWidget(self.btn_reset) main_layout.addLayout(control_layout, 1) def update_method(self): """更新连接点计算方法""" method = self.method_combo.currentData() self.chart_view.update_lines(method) def randomize_data(self): """随机更新饼图数据""" series = self.chart_view.series total = 0 new_values = [] # 生成随机值(确保总和为100) for _ in range(series.count() - 1): value = random.randint(5, 40) new_values.append(value) total += value last_value = 100 - total new_values.append(last_value if last_value > 0 else 5) # 更新扇区值 for i, slice in enumerate(series.slices()): slice.setValue(new_values[i]) def reset_data(self): """重置为初始数据""" values = [30, 20, 25, 15, 10] for i, slice in enumerate(self.chart_view.series.slices()): slice.setValue(values[i]) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` ## 解决方案说明 ### 错误修复关键点 1. **避免使用 labelPosition() 作为坐标**: ```python # 错误方式(返回枚举值) label_pos = slice.labelPosition() # 返回 0,1,2,3 不是坐标 # 正确方式1:使用标签几何中心 label_rect = slice.labelGeometry() label_center = label_rect.center() # 正确方式2:使用三角函数计算 mid_angle = slice.startAngle() + slice.angleSpan() / 2 radian = math.radians(mid_angle) edge_point = QPointF( center.x() + radius * math.cos(radian), center.y() - radius * math.sin(radian) ) ``` 2. **坐标系统注意事项**: - Qt 使用左上角为原点的坐标系(Y轴向下) - 三角函数计算时 Y 坐标需要取负值 - 所有坐标点都是基于场景坐标(Scene Coordinates) ### 高级功能实现 1. **两种计算方法**: - 三角函数计算:更精确,连接点在扇区边缘中点 - 标签中心位置:连接点在标签中心,可能与扇区边缘有偏差 2. **视觉增强**: - 红色中心点和彩色边缘点标记 - 线条颜色与扇区颜色匹配 - 抗锯齿渲染提高视觉质量 3. **交互功能**: - 随机数据生成 - 数据重置功能 - 计算方法切换 4. **错误预防**: - 确保随机数据总和为100 - 清理旧图形项防止内存泄漏 - 使用抗锯齿提高渲染质量 ## 常见问题及解决方法 1. **线条位置不正确**: - 检查坐标系转换是否正确 - 确认使用了 `plotArea().center()` 而不是 `chart().center()` - 验证角度计算:`startAngle()` + `angleSpan()/2` 2. **线条不随图表更新**: - 确保连接了正确的信号: ```python self.series.countChanged.connect(self.update_lines) self.chart.plotAreaChanged.connect(self.update_lines) ``` 3. **性能问题**: - 避免在 `paint` 方法中进行复杂计算 - 使用 `QGraphicsLineItem` 而不是自定义绘制 - 清理旧图形项防止内存泄漏 4. **标签不可见**: - 确保设置了 `slice.setLabelVisible(True)` - 检查标签位置:`slice.setLabelPosition(QPieSlice.LabelOutside)` - 调整标签臂长度:`slice.setLabelArmLengthFactor(0.2)`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值