PyEcharts 直角坐标系图表之折线/面积图

本文通过八个实战案例,详细介绍如何使用PyEcharts库绘制不同类型的折线图,包括基本配置、面积图、样式定制、对数轴应用、多X轴设置、数据分布标记等高级功能。

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

目录

第1关:Line:折线图(一)

第2关:Line:折线图(二)

第3关:Line:折线图(三)

第4关:Line:折线图(四)

第5关:Line:折线图(五)

第6关:Line:折线图(六)

第7关:Line:折线图(七)

第8关:Line:折线图(八)


第1关:Line:折线图(一)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line


x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y_data = [820, 932, 901, 934, 1290, 1330, 1320]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line()
        .add_xaxis(x_data)
        .add_yaxis("",y_data,symbol='emptyCircle',is_symbol_show=True,label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            xaxis_opts=opts.AxisOpts(
                type_="category"
            ),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True)
            ),
        )
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_base(x_data, y_data).render(), "StandardAnswer/task1/standard_answer_1.png")

第2关:Line:折线图(二)

from PreTest import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

data_x = Faker.choose()
data_y_1 = Faker.values()
data_y_2 = Faker.values()


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line()
        .add_xaxis(data_x)
        .add_yaxis("商家A",data_y_1)
        .add_yaxis("商家B",data_y_2)
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Line-面积图")
            
        )
        .set_series_opts(
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
        )
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, line_area_style(data_x, data_y_1, data_y_2).render(), "StandardAnswer/task2/standard_answer_2.png")

第3关:Line:折线图(三)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

from pyecharts.faker import Faker

data_x = Faker.choose()
data_y = Faker.values()


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line()
        .add_xaxis(data_x)
        .add_yaxis(
            "商家A",
            data_y,
            symbol_size=20,
            symbol='triangle',
            linestyle_opts=opts.LineStyleOpts(width=4,type_='dashed',color='green'),
            itemstyle_opts=opts.ItemStyleOpts(border_width=3,border_color="yellow",color='blue')
            )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Line-ItemStyle")
        )

    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_itemstyle(data_x, data_y).render(), "StandardAnswer/task3/standard_answer_3.png")

第4关:Line:折线图(四)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

x_data = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
y_data_3 = [1, 3, 9, 27, 81, 247, 741, 2223, 6669]
y_data_2 = [1, 2, 4, 8, 16, 32, 64, 128, 256]
y_data_05 = [1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64, 1 / 128, 1 / 256, 1 / 512]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add_xaxis(xaxis_data=x_data)
        .add_yaxis(
            series_name="1/2的指数",
            y_axis=y_data_05,
            linestyle_opts=opts.LineStyleOpts(width=2),
        )
        .add_yaxis(
            series_name="2的指数", y_axis=y_data_2, linestyle_opts=opts.LineStyleOpts(width=2)
        )
        .add_yaxis(
            series_name="3的指数", y_axis=y_data_3, linestyle_opts=opts.LineStyleOpts(width=2)
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="对数轴示例", pos_left="center"),
            tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}"),
            legend_opts=opts.LegendOpts(pos_left="left"),
            xaxis_opts=opts.AxisOpts(type_="category", name="x"),
            yaxis_opts=opts.AxisOpts(
                type_="log",
                name="y",
                splitline_opts=opts.SplitLineOpts(is_show=True),
                is_scale=True,
            ),
        )
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_log_axis(x_data, y_data_05, y_data_2, y_data_3).render(), "StandardAnswer/task4/standard_answer_4.png")

第5关:Line:折线图(五)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.commons.utils import JsCode

from pyecharts.charts import Line

js_formatter = """function (params) {
        console.log(params);
        return '降水量  ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
    }"""


data_x_1 = [
    "2016-1",
    "2016-2",
    "2016-3",
    "2016-4",
    "2016-5",
    "2016-6",
    "2016-7",
    "2016-8",
    "2016-9",
    "2016-10",
    "2016-11",
    "2016-12",
]

data_x_2 = [
    "2015-1",
    "2015-2",
    "2015-3",
    "2015-4",
    "2015-5",
    "2015-6",
    "2015-7",
    "2015-8",
    "2015-9",
    "2015-10",
    "2015-11",
    "2015-12",
]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
    .add_xaxis(data_x_1)
    .extend_axis(
        xaxis_data=data_x_2,
        xaxis=opts.AxisOpts(
            type_="category",
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            axisline_opts=opts.AxisLineOpts(
                is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1")
            ),
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
            ),
        ),
    )
    .add_yaxis(
        series_name="2015 降水量",
        is_smooth=True,
        symbol="emptyCircle",
        is_symbol_show=False,
        # xaxis_index=1,
        color="#d14a61",
        y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
        label_opts=opts.LabelOpts(is_show=False),
        linestyle_opts=opts.LineStyleOpts(width=2),
    )
    .add_yaxis(
        series_name="2016 降水量",
        is_smooth=True,
        symbol="emptyCircle",
        is_symbol_show=False,
        color="#6e9ef1",
        y_axis=[3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7],
        label_opts=opts.LabelOpts(is_show=False),
        linestyle_opts=opts.LineStyleOpts(width=2),
    )
    .set_global_opts(
        legend_opts=opts.LegendOpts(),
        tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            axisline_opts=opts.AxisLineOpts(
                is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
            ),
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
            ),
        ),
        yaxis_opts=opts.AxisOpts(
            type_="value",
            splitline_opts=opts.SplitLineOpts(
                is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
            ),
        ),
    )

    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_multiple_x_axis(data_x_1, data_x_2, js_formatter).render(), "StandardAnswer/task5/standard_answer_5.png")

第6关:Line:折线图(六)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line


x_data = [
    "00:00",
    "01:15",
    "02:30",
    "03:45",
    "05:00",
    "06:15",
    "07:30",
    "08:45",
    "10:00",
    "11:15",
    "12:30",
    "13:45",
    "15:00",
    "16:15",
    "17:30",
    "18:45",
    "20:00",
    "21:15",
    "22:30",
    "23:45",
]
y_data = [
    300,
    280,
    250,
    260,
    270,
    300,
    550,
    500,
    400,
    390,
    380,
    390,
    400,
    500,
    600,
    750,
    800,
    700,
    600,
    400,
]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add_xaxis(x_data)
        .add_yaxis(
            series_name="用电量",
            y_axis=y_data,
            is_smooth=True,
            label_opts=opts.LabelOpts(is_show=False),
            linestyle_opts=opts.LineStyleOpts(width=2),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="一天用电量分布", subtitle="纯属虚构"),
            tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            xaxis_opts=opts.AxisOpts(boundary_gap=False),
            yaxis_opts=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(formatter="{value} W"),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            visualmap_opts=opts.VisualMapOpts(
                is_piecewise=True,
                dimension=0,
                pieces=[
                    {"lte": 6, "color": "green"},
                    {"gt": 6, "lte": 8, "color": "red"},
                    {"gt": 8, "lte": 14, "color": "green"},
                    {"gt": 14, "lte": 17, "color": "red"},
                    {"gt": 17, "color": "green"},
                ],
            ),
        )
        .set_series_opts(
            markarea_opts=opts.MarkAreaOpts(
                data=[
                    opts.MarkAreaItem(name="早高峰", x=("07:30", "10:00")),
                    opts.MarkAreaItem(name="晚高峰", x=("17:30", "21:15")),
                ]
            )
        )
    )

    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_distribution_of_electricity(x_data, y_data).render(), "StandardAnswer/task6/standard_answer_6.png")

第7关:Line:折线图(七)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

from pyecharts.faker import Faker

week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add_xaxis(xaxis_data=week_name_list)
        .add_yaxis(
            series_name="最高气温",
            y_axis=high_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
        .add_yaxis(
            series_name="最低气温",
            y_axis=low_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[
                    opts.MarkLineItem(type_="average", name="平均值"),
                    opts.MarkLineItem(symbol="none", x="90%", y="max"),
                    opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
                ]
            ),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="未来一周气温变化", subtitle="纯属虚构"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )

    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_temperature_change(week_name_list, high_temperature, low_temperature).render(), "StandardAnswer/task7/standard_answer_7.png")

第8关:Line:折线图(八)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot


from pyecharts.charts import Line

x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]

y_data_1 = [120, 132, 101, 134, 90, 230, 210]
y_data_2 = [220, 182, 191, 234, 290, 330, 310]
y_data_3 = [150, 232, 201, 154, 190, 330, 410]
y_data_4 = [320, 332, 301, 334, 390, 330, 320]
y_data_5 = [820, 932, 901, 934, 1290, 1330, 1320]

def line_chart() -> Line:
    # ********* Begin *********#  
    line = (

        Line()
        .add_xaxis(xaxis_data=x_data)
        .add_yaxis(
            series_name="邮件营销",
            stack="总量",
            y_axis=[120, 132, 101, 134, 90, 230, 210],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="联盟广告",
            stack="总量",
            y_axis=[220, 182, 191, 234, 290, 330, 310],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
            .add_yaxis(
            series_name="视频广告",
            stack="总量",
            y_axis=[150, 232, 201, 154, 190, 330, 410],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="直接访问",
            stack="总量",
            y_axis=[320, 332, 301, 334, 390, 330, 320],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="搜索引擎",
            stack="总量",
            y_axis=[820, 932, 901, 934, 1290, 1330, 1320],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="堆叠区域图"),
            tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_stacked_area(x_data, y_data_1, y_data_2, y_data_3, y_data_4, y_data_5).render(), "StandardAnswer/task8/standard_answer_8.png")

<think>好的,我现在需要帮助用户解决在Kali系统中安装Docker Compose时遇到的错误,特别是APT仓库未签名的问题。用户提到错误信息是“仓库未签名 buster InRelease”。首先,我应该回忆一下之前提供的安装步骤,以及用户现在遇到的问题可能出在哪里。 根据用户之前的对话,我之前给出了安装Docker Compose的方法是通过下载二进制文件,但用户现在可能尝试通过APT仓库安装,或者是在安装Docker时遇到了仓库签名的问题。用户引用了几个不同的源,包括官方Docker仓库清华镜像,可能用户尝试了不同的仓库导致签名错误。 首先,我需要确定用户的具体操作步骤。用户可能在添加Docker仓库到Kali后,运行sudo apt update时遇到了签名错误,因为Kali基于Debian,但Docker的官方仓库可能没有正确的签名密钥被系统信任。常见的错误可能是“NO_PUBKEY”或者“签名无效”。 解决方案应该是导入Docker的官方GPG密钥,以验证仓库的签名。根据引用中的信息,用户可能已经按照步骤添加了仓库,但缺少了密钥的导入步骤。例如,在引用[2]中提到添加软件源的GPG密钥,可能用户漏掉了这一步。 接下来,我需要检查正确的步骤是否包括下载并添加GPG密钥。通常,添加APT仓库后需要导入对应的公钥,否则会提示未签名的问题。因此,我应该建议用户运行如下的命令来导入密钥: curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker.gpg 这应该能解决仓库未签名的问题。如果用户之前使用了不同的镜像源,比如清华大学的镜像,可能需要调整仓库URL对应的密钥,但通常Docker的官方密钥适用于所有镜像。 另外,用户可能使用了旧版本的Kali(如基于Debian buster),而Docker的仓库可能已经更新,需要确认仓库配置是否正确。例如,确保/etc/apt/sources.list.d/docker.list中的发行版名称(如buster)与系统兼容。 如果用户仍然遇到问题,可能需要清除旧的仓库信息,重新添加并更新缓存。或者检查是否有拼写错误在仓库配置文件中。 此外,用户提到安装Docker Compose,我之前建议的是下载二进制文件的方式,但用户可能尝试通过APT安装docker-compose-plugin,这可能会遇到依赖问题或者版本不兼容。需要指出在Kali中推荐使用二进制安装方式,因为APT源中的版本可能较旧或有依赖问题。 最后,应该提醒用户在解决问题后,重新运行sudo apt update安装docker-compose的步骤,或者继续使用二进制安装方法。同时,处理可能残留的旧配置或缓存,比如清除/var/lib/apt/lists/中的部分文件。</think>### Kali系统解决Docker Compose安装错误及APT仓库签名问题 #### 一、修复APT仓库未签名问题 当出现`仓库未签名 buster InRelease`错误时,说明系统未信任Docker仓库的GPG密钥。执行以下操作: 1. **清理旧仓库配置**(若存在冲突): ```bash sudo rm -f /etc/apt/sources.list.d/docker.list* ``` 2. **添加正确仓库源**(任选一种): - **阿里云镜像加速版**: ```bash echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian buster stable" | sudo tee /etc/apt/sources.list.d/docker.list ``` - **官方仓库版**: ```bash echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/debian buster stable" | sudo tee /etc/apt/sources.list.d/docker.list ``` 3. **手动导入GPG密钥**(核心步骤): ```bash sudo mkdir -p /usr/share/keyrings curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg ``` 4. **强制更新仓库缓存**: ```bash sudo apt update --allow-insecure-repositories ``` #### 二、安装Docker Compose ##### 方案一:推荐二进制安装(绕过APT依赖问题) 1. **下载最新版二进制文件**: ```bash sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.7/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose ``` 2. **赋权并验证**: ```bash sudo chmod +x /usr/local/bin/docker-compose docker-compose --version # 应输出版本号 ``` ##### 方案二:APT安装(需先修复签名) ```bash sudo apt install docker-compose-plugin docker compose version # 注意新版命令格式 ``` #### 三、典型错误处理 1. **密钥错误补充方案**: ```bash # 若仍提示NO_PUBKEY,手动添加密钥 sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7EA0A9C3F273FCD8 ``` 2. **残留缓存清理**: ```bash sudo rm -rf /var/lib/apt/lists/partial/* sudo apt clean && sudo apt update ``` #### 四、验证完整工作流 ```bash # 创建测试docker-compose.yml echo 'version: "3" services: hello-world: image: hello-world' > docker-compose.yml # 运行测试 docker-compose up ``` --- ### 关键点解析 1. **签名问题本质**:Kali基于Debian,但未预装Docker的GPG密钥,需通过`--signed-by`参数显式指定密钥路径[^2][^3] 2. **架构兼容性**:必须确认`[arch=amd64]`与当前系统架构匹配(Kali默认使用amd64) 3. **版本对应关系**:`buster`对应Debian 10,需确保仓库声明与系统版本一致[^1][^4] --- ### 相关问题 1. 如何查看Kali Linux的Debian基础版本? 2. Docker Compose二进制安装与APT安装有何区别? 3. 如何彻底卸载Docker及残留配置? 4. 为什么国内推荐使用镜像加速仓库?
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值