Qt数据可视化(QBoxPlotSeries盒须图)

article/2025/8/26 23:55:15

目录

创建盒须图类

设置盒须图数据

计算中间值

源代码

widget.cpp


实现效果如下:

 

QBoxPlotSeries类以方框和胡须图表的形式显示数据。

“长方体绘图”系列充当长方体和胡须项目的容器。多个系列中的项目根据其索引值分组。

QBarCategoryAxis类用于将类别添加到图表的轴。类别标签必须是唯一的。如果为多个长方体和胡须项目定义了相同的类别标签,则只绘制第一个类别标签。

盒须图最主要设置为QBarSet的设置,详细信息如下表:

QBoxSet::LowerExtreme0盒须图最小值
QBoxSet::LowerQuartile1盒须图中间部分的最小值
QBoxSet::Median2盒须图中间值
QBoxSet::UpperQuartile3盒须图中间部分的最大值
QBoxSet::UpperExtreme4盒须图最大值

如下图: 

创建盒须图类

通过new创建两个盒须图类,可以通过setname设置其名字.

  m_ptrBoxPlotSeries(new QBoxPlotSeries)m_ptrBoxPlotSeries2(new QBoxPlotSeries)
  m_ptrBoxPlotSeries->setName("统信");m_ptrBoxPlotSeries2->setName("麒麟");

设置盒须图数据

数据设置使用QBarSet进行设置.

因为QBarSet的设置有大小之分,所以我们可以使用qSort对数据进行排序.

那么最小值为listData.first().最大值为listData.last().

中间值为listData的中间值.

QList<qreal> listData;listData << 25.74 << 25.28 << 25.86 << 26.05 << 26.64 << 28.47 << 28.30<< 29.22 << 29.72 << 27.50 << 25.62 << 25.50 << 25.15 << 26.47<< 27.41 << 25.78 << 24.82 << 24.89 << 24.88 << 24.60 << 23.85;qSort(listData.begin(), listData.end());QBoxSet *box = new QBoxSet("统信");int count = listData.count();box->setValue(QBoxSet::LowerExtreme, listData.first());box->setValue(QBoxSet::UpperExtreme, listData.last());box->setValue(QBoxSet::Median, findMedian(listData, 0, count));box->setValue(QBoxSet::LowerQuartile, findMedian(listData, 0, count / 2));box->setValue(QBoxSet::UpperQuartile,findMedian(listData, count / 2 + (count % 2), count));m_ptrBoxPlotSeries->append(box);

计算中间值

传入列表数据,将计算,起点和终点之间的中间值.如果%2 为1那么说明起为单数,可以使用二分之一总数+起始位置.

如果是双数,计算左右值的和除以2.

qreal Widget::findMedian(QList<qreal> sortedList, int begin, int end)
{//! [5]int count = end - begin;if (count % 2) {return sortedList.at(count / 2 + begin);} else {qreal right = sortedList.at(count / 2 + begin);qreal left = sortedList.at(count / 2 - 1 + begin);return (right + left) / 2.0;}//! [5]
}

源代码

源代码地址git地址

​​​​​​QtChartDemo/QBoxPlotSeries/BoxPlotSeries · master · 啊渊 / QT博客案例 · GitCode

源代码

widget.cpp

#include "widget.h"
#include <QtDebug>
#include <QLegendMarker>
#include <QBarLegendMarker>
Widget::Widget(QWidget *parent): QWidget(parent), m_ptrChart(new QChart), m_ptrChartview(new QChartView(m_ptrChart, this)), m_ptrBoxPlotSeries(new QBoxPlotSeries), m_ptrBoxPlotSeries2(new QBoxPlotSeries)
{initUI();initConnect();
}
void Widget::initUI()
{setWindowTitle(tr("盒须图样例"));m_ptrChart->setMinimumSize(500, 500);m_ptrChart->setTitle("盒须图样例");initData();m_ptrChart->createDefaultAxes();m_ptrChart->axes(Qt::Vertical).first()->setMin(15.0);m_ptrChart->axes(Qt::Vertical).first()->setMax(40.0);m_ptrChart->setAnimationOptions(QChart::AllAnimations);m_ptrChartview->setFixedSize(600, 600);m_ptrChartview->setRenderHint(QPainter::Antialiasing);
}void Widget::initConnect()
{foreach (auto marker, m_ptrChart->legend()->markers()) {QObject::disconnect(marker, &QLegendMarker::clicked, this,&Widget::handleMarkerClicked);QObject::connect(marker, &QLegendMarker::clicked, this,&Widget::handleMarkerClicked);}
}
void Widget::handleMarkerClicked()
{QLegendMarker *marker = qobject_cast<QLegendMarker *>(sender());//断言Q_ASSERT(marker);switch (marker->type()) {case QLegendMarker::LegendMarkerTypeBoxPlot: {//控序列隐藏/显示// Toggle visibility of seriesmarker->series()->setVisible(!marker->series()->isVisible());// Turn legend marker back to visible, since hiding series also// hides the marker and we don't want it to happen now.marker->setVisible(true);//修改图例// Dim the marker, if series is not visibleqreal alpha = 1.0;if (!marker->series()->isVisible()) alpha = 0.5;QColor color;QBrush brush = marker->labelBrush();color = brush.color();color.setAlphaF(alpha);brush.setColor(color);marker->setLabelBrush(brush);brush = marker->brush();color = brush.color();color.setAlphaF(alpha);brush.setColor(color);marker->setBrush(brush);QPen pen = marker->pen();color = pen.color();color.setAlphaF(alpha);pen.setColor(color);marker->setPen(pen);break;}default: {qInfo() << "Unknown marker type";break;}}
}
void Widget::initData()
{QList<qreal> listData;listData << 25.74 << 25.28 << 25.86 << 26.05 << 26.64 << 28.47 << 28.30<< 29.22 << 29.72 << 27.50 << 25.62 << 25.50 << 25.15 << 26.47<< 27.41 << 25.78 << 24.82 << 24.89 << 24.88 << 24.60 << 23.85;qSort(listData.begin(), listData.end());QBoxSet *box = new QBoxSet("统信");int count = listData.count();box->setValue(QBoxSet::LowerExtreme, listData.first());box->setValue(QBoxSet::UpperExtreme, listData.last());box->setValue(QBoxSet::Median, findMedian(listData, 0, count));box->setValue(QBoxSet::LowerQuartile, findMedian(listData, 0, count / 2));box->setValue(QBoxSet::UpperQuartile,findMedian(listData, count / 2 + (count % 2), count));m_ptrBoxPlotSeries->append(box);listData.clear();qInfo() << listData;listData << 30.79 << 29.62 << 29.67 << 30.37 << 30.16 << 30.22 << 31.02<< 33.70 << 32.60 << 32.24 << 31.98 << 31.79 << 31.10 << 30.79<< 31.53 << 30.92 << 29.00 << 29.58 << 30.37 << 29.40 << 28.60;qSort(listData.begin(), listData.end());QBoxSet *box2 = new QBoxSet("麒麟");count = listData.count();qInfo() << findMedian(listData, 0, count);qInfo() << findMedian(listData, 0, count / 2);qInfo() << findMedian(listData, count / 2 + (count % 2), count);box2->setValue(QBoxSet::LowerExtreme, listData.first());box2->setValue(QBoxSet::UpperExtreme, listData.last());box2->setValue(QBoxSet::Median, findMedian(listData, 0, count));box2->setValue(QBoxSet::LowerQuartile, findMedian(listData, 0, count / 2));box2->setValue(QBoxSet::UpperQuartile,findMedian(listData, count / 2 + (count % 2), count));m_ptrBoxPlotSeries2->append(box2);m_ptrBoxPlotSeries->setName("统信");m_ptrBoxPlotSeries2->setName("麒麟");m_ptrChart->addSeries(m_ptrBoxPlotSeries);m_ptrChart->addSeries(m_ptrBoxPlotSeries2);
}
qreal Widget::findMedian(QList<qreal> sortedList, int begin, int end)
{//! [5]int count = end - begin;if (count % 2) {return sortedList.at(count / 2 + begin);} else {qreal right = sortedList.at(count / 2 + begin);qreal left = sortedList.at(count / 2 - 1 + begin);return (right + left) / 2.0;}//! [5]
}
Widget::~Widget() {}

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QChart>
#include <QChartView>
#include <QBoxPlotSeries>
#include <QBoxSet>
QT_CHARTS_USE_NAMESPACE
class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();void initUI();void initConnect();void initData();qreal findMedian(QList<qreal> sortedList, int begin, int end);public slots:void handleMarkerClicked();private:QChart *m_ptrChart;QChartView *m_ptrChartview;QBoxPlotSeries *m_ptrBoxPlotSeries;QBoxPlotSeries *m_ptrBoxPlotSeries2;
};#endif  // WIDGET_H


http://chatgpt.dhexx.cn/article/0VYS6ig9.shtml

相关文章

八、Echart图表 之 series盒须图(箱体图)基本使用与配置大全

&#x1f353; 作者主页&#xff1a;&#x1f496;仙女不下凡&#x1f496; &#x1f353; 前言介绍&#xff1a;以下&#x1f447;内容是我个人对于该技术的总结&#xff0c;如有不足与错误敬请指正&#xff01; &#x1f353; 欢迎点赞&#x1f44d; 收藏⭐ 留言&#x1f4…

保姆式教学:用Tableau制作盒须图(箱线图)

盒须图&#xff08;箱线图&#xff09;是一种常用的统计图形&#xff0c;用来显示数据的位置、分散程度和异常值等。箱线图主要包括6个统计量&#xff1a;下线、第一四分位数、中位数、第三四分位数、上限和异常值。通过绘制盒须图&#xff0c;观测数据在同类群体中的位置&…

Tableau:如何添加参考线、趋势线、参考区间、分布区间、盒须图?

序言 Tableau中的“分析”栏提供了很多功能(如下图)&#xff0c;这些功能可以向Tableau的工作表添加各种辅助线和辅助区间。通过对其进行归类&#xff0c;可以将这些辅助线、辅助区间分为&#xff1a;参考线、参考区间、分布区间、盒须图及趋势线等。下面依次进行介绍。 1. 参…

Qt图表绘制(QtCharts)-绘制简单的盒须图[箱形图](12)

Qt图表绘制&#xff08;QtCharts&#xff09;-绘制简单的盒须图[箱形图]&#xff08;12&#xff09;✌ 文章目录 Qt图表绘制&#xff08;QtCharts&#xff09;-绘制简单的盒须图[箱形图]&#xff08;12&#xff09;✌1、概述&#x1f90f;2、实现步骤&#x1f91e;3、主要使用的…

Qt散点图、折线图、柱状图、盒须图、饼状图、雷达图开发实例

目录 散点图 折线图 柱状图 水平柱状图 水平堆叠图 水平百分比柱状图 盒须图 饼状图 雷达图 Qt散点图、折线图、柱状图、盒须图、饼状图、雷达图开发实例。 在开发过程中我们会使用多各种各样的图&#xff0c;讲数据进行可视化。我们可以使用以上几种图来表达我们的数…

Tableau-盒须图

Tableau-盒须图 前言1.1 数据拖拽1.2 智能推荐-盒须图1.3 结果展示 前言 盒须图的制作方便我们直观观察数据分布情况。 对应业务场景&#xff0c;例如餐饮数据&#xff0c;我们可以通过盒须图查看不同店铺在不同平台上GMV总和数据信息。 本文的数据较少&#xff0c;不能很好的…

盒须图简介

2019独角兽企业重金招聘Python工程师标准>>> 盒须图&#xff08;Boxplot&#xff09;主要用于对数据分布的显示。对于详细数据的显示通常采用下面几种方法。 最简单的方法是把所有的数据显示在一个散点图上。读者可以直接观察数据点的分布。 但如果数据量很大&#…

数据分散情况的统计图-盒须图

数据可视化分析时还常常需要观察数据的分布状态&#xff0c;或者查看某一个个体在整体的表现如何&#xff0c;这都需要用到统计分布图&#xff0c;盒须图就是其中的一种。 盒须图它可以用来反映一组或者多组数据的分布情况&#xff0c;因形状像长着胡须的盒子而得名。 盒须图…

Tableau图表 • 盒须图、抖动图

盒须图&#xff0c;又叫箱线图&#xff0c;是一种用作显示一组数据分散情况资料的统计图。盒须图能够比较直观的显示数据集的分散程度、异常值等信息。 盒须图 盒须图基础 盒须图包括六个统计量&#xff1a;最小值&#xff0c;下四分位数(Q1)&#xff0c;中位数&#xff0c;…

22.23.24.25.盒须图(boxplot)、棉棒图(Stem Plot; Lollipop plot)、极坐标图、雷达图(Radar Chart)

22.盒须图&#xff08;boxplot&#xff09; 23.棉棒图(Stem Plot; Lollipop plot) 24.极坐标图 25.雷达图&#xff08;Radar Chart&#xff09; 22.盒须图&#xff08;boxplot&#xff09; 盒须图(也称为箱形图&#xff09;是一种图表类型&#xff0c;通常用于说明性数据分析…

PS图片中字体或图像的颜色替换

第一步、打开替换颜色修改框。步骤&#xff1a;图像——调整——替换颜色 第二部、选择要替换的颜色 完后点确定&#xff0c;就可以修改好自己想要的颜色。

如何使用PS改变只有一种颜色图片的颜色

这里所说的图片是这种单色的图片 首先将这张图片拖到PS中打开&#xff0c;选择 图层 > 图层样式 > 颜色叠加 然后我们选择一种颜色 颜色改完了&#xff0c;保存。

ps修改图片的颜色

作为一个啥都要会的前端&#xff0c;有时候UI给了图&#xff0c;都是单色黑的&#xff0c;说需要什么颜色自己调&#xff0c;so&#xff1a; 1,新建一个200*200像素的画布(和图片一样大的画布) 2,将图片拖入画布 3,在右侧图层上双击&#xff0c;出现对话框 4,双击对话框左侧菜单…

PS修改图片的背景颜色(无需抠图)

1.复制图层&#xff0c;可以鼠标右键复制图层或CtrlJ复制图层 2.在菜单栏找到图像-->调整-->替换颜色&#xff0c;此时鼠标箭头变成了一个吸管的样子&#xff0c;去吸一下想要替换的颜色&#xff0c;然后选择要修改的颜色即可 具体如下操作&#xff1a;

不是美工,如何使用ps快速更换图标icon的颜色?

最近做个项目&#xff0c;美工撂挑子不干了&#xff0c;需要处理一个icon的颜色&#xff0c;怎么办呢&#xff1f;关键时刻只有靠自己了&#xff0c;自己上手。 使用ps就是简单&#xff0c;把制作方法分享给大家&#xff0c;欢迎留言讨论&#xff0c;自己也做个备忘。 目 录…

ps修改图片中的文字、数字

刚才学习修改ps图片中的文字&#xff0c;找到知乎这位博主写的&#xff0c;很详细&#xff0c;下面转载一下&#xff01;&#xff01; https://zhuanlan.zhihu.com/p/134102174 以下内容&#xff0c;是根据视频教程边学习&#xff0c;边做的笔记ヾ(o◕∀◕)&#xff89;ヾ 讲…

PS如何修改图片日期或者其他文本内容

Spraing※boy作为一个修图软件&#xff0c;PS的功能强大到毫无人性&#xff0c;我也在逐渐探索这个神器&#xff0c;奢望着有一天能够征服它&#xff0c;给自己也P上黄金万两。这篇博客我们主要来探索一下PS如何修改图片上的日期或者其他的文本内容。 准备阶段&#xff1a;PS软…

ps改变图片色调

作u3d&#xff0c;还要稍微会一点ps的东西。 要改变图片文字或者其他整块颜色块的的颜色&#xff0c;首先 然后第二步&#xff1a; 减去多余的选区 最后&#xff0c;用油漆桶涂上其他颜色

ps改变图片的颜色

用PS打开图片选中你要改变颜色的区域&#xff0c;然后点击&#xff1a;图像-调整-颜色替换 然后选中中间的吸管&#xff0c;随意吸一个你所选区域&#xff0c;再调整色相&#xff0c;饱和度&#xff0c;明度 转载于:https://www.cnblogs.com/ccpblo/p/7009395.html

ps修改图片中的图片尺寸

今天有个小需求&#xff0c;需要修改图片中一个小icon的大小&#xff0c;统一修改为32*32 简单自学了一下ps&#xff0c;把方法分享给大家。 需要修改的图片文件为&#xff1a; 把icon修改为32*32尺寸大小。 ps具体操作如下&#xff1a; 1.使用ps打开需要修改的png文件&…