Software Testing | Python Data Visualization Artifact - pyecharts Tutorial (11)

Use Pyecharts to draw instrument charts - advanced display of data visualization

Instrument diagram introduction

An instrument chart is a data visualization icon, similar to the instrument on a car dashboard. It displays key data and indicators in the form of charts, tables, indicators, etc., so that users can quickly understand and analyze the status, trends and correlations of the data. It is usually represented by Presented in the form of a dashboard.

Dashboard charts provide intuitive, concise and easy-to-understand data display, helping us quickly capture key information and insights. Through dashboard charts, we can monitor business indicators, data trends and key performance indicators in real time, making it easier for us to make quick decisions.

As an artifact of data visualization, Pyecharts can of course also draw instrument charts. This article will introduce how to use Pyecharts to draw instrument charts.

Basic configuration of instrument chart class

  1. Guide package

In Pyecharts, each type of diagram has a different class, and instrument diagrams are no exception. The class of instrument diagrams is. GaugeNow let’s import the package. This is the first step in drawing instrument diagrams:

from pyecharts import options as opts
from pyecharts.charts import Gauge
  1. add function configuration

After we import the class, we need to consider the specific configuration of the instrument chart. The add function is the function we use to configure the instrument chart. We can use different parameters to set the instrument chart. By viewing the source code, we can find Parameter description is as follows:

def add(
        self,
        series_name: str,  # 系列名称,字符串类型
        data_pair: types.Sequence,  # 数据对,序列类型
        *,
        min_: types.Numeric = 0,  # 最小值,默认为0
        max_: types.Numeric = 100,  # 最大值,默认为100
        split_number: types.Numeric = 10,  # 分割段数,默认为10
        center: types.Sequence = None,  # 中心位置,默认为空
        radius: types.Union[types.Numeric, str] = "75%",  # 半径,默认为"75%"
        start_angle: types.Numeric = 225,  # 起始角度,默认为225
        end_angle: types.Numeric = -45,  # 结束角度,默认为-45
        is_clock_wise: bool = True,  # 是否顺时针,默认为True
        title_label_opts: types.GaugeTitle = opts.GaugeTitleOpts(  # 仪表盘标题配置,默认为指定的配置
            offset_center=["0%", "20%"],
        ),
        detail_label_opts: types.GaugeDetail = opts.GaugeDetailOpts(  # 仪表盘详细标签配置,默认为指定的配置
            formatter="{value}%",
            offset_center=["0%", "40%"],
        ),
        progress: types.GaugeProgress = opts.GaugeProgressOpts(),  # 仪表盘进度配置,默认为指定的配置
        pointer: types.GaugePointer = opts.GaugePointerOpts(),  # 仪表盘指针配置,默认为指定的配置
        anchor: types.GaugeAnchor = opts.GaugeAnchorOpts(),  # 仪表盘锚点配置,默认为指定的配置
        tooltip_opts: types.Tooltip = None,  # 提示框配置,默认为空
        axisline_opts: types.AxisLine = None,  # 坐标轴线配置,默认为空
        axistick_opts: types.AxisTick = None,  # 坐标轴刻度配置,默认为空
        axislabel_opts: types.AxisLabel = None,  # 坐标轴标签配置,默认为空
        itemstyle_opts: types.ItemStyle = None,  # 数据项样式配置,默认为空
    )

Drawing practice

  1. Draw basic instrument diagram

We'll demonstrate how to create a simple instrument chart. Suppose we want to display the battery percentage of a certain battery. The code is as follows:

from pyecharts import options as opts
from pyecharts.charts import Gauge


gauge = (
    Gauge()
    .add("", [("电量", 80)])  # 标题和数据
    .set_global_opts(title_opts=opts.TitleOpts(title="电池电量仪表图"))
)
gauge.render("basic_gauge.html")


Run the script and open the generated html file in the browser, as follows:

Insert image description here

  1. Customization and beautification of instrument diagrams

Above we just drew a basic instrument diagram, but in real life, such as the dashboard of a car, the speed is represented by different colors. For example, the scale close to the top speed is red, and the font can be modified, etc. Now Let's optimize the dashboard.

from pyecharts import options as opts
from pyecharts.charts import Gauge


gauge = (
    Gauge()
    .add("", [("电量", 80)], split_number=5)
    .set_global_opts(
        title_opts=opts.TitleOpts(title="电池电量仪表图", subtitle="示例"),
        legend_opts=opts.LegendOpts(is_show=False),
    )
    .set_series_opts(
        axisline_opts=opts.AxisLineOpts(
            linestyle_opts=opts.LineStyleOpts(

                color=[[0.2, "#c23531"], [0.8, "#63869e"], [1, "#91c7ae"]],
                width=30,
            )
        ),
        splitline_opts=opts.SplitLineOpts(

            is_show=True,
            linestyle_opts=opts.LineStyleOpts(width=3, color="auto"),
        ),
    )
)
gauge.render("customized_gauge.html")

In this example, we added a title and subtitle, hidden the legend, changed the color of the dashboard, and adjusted the style of the tick marks. We ran the script and the generated HTML file was opened in the browser as shown below:

Insert image description here

Summarize

Using PyechartsGauges is a powerful way to visualize data for a single metric. This article explains how to get started with Pyechartscreating a basic instrument diagram and demonstrates how to customize and embellish it to suit our needs. By gaining a deep understanding of Pyecharts' features and options, we can create impressive dashboards to better communicate and share the results of our data analysis. Start using Pyecharts to make your data more convincing and attractive!

To receive more artificial intelligence learning materials, please click!

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132982284