可视化pyecharts绘制折线图

可视化pyecharts绘制折线图,为什么y轴信息出不来,画出以下图。

img


import pandas as pd
from pyecharts.charts import Line
from pyecharts import options as opts
import matplotlib.pyplot as plt
from datetime import datetime

data = pd.read_excel('itemtoge.xlsx')

# 筛选美国金牌、银牌、铜牌数据
usa_gold = data[(data['名次'] == 1)&(data['国家'] == '美国')]
usa_silver = data[(data['名次'] == 2)&(data['国家'] == '美国')]
usa_bronze = data[(data['名次'] == 3)&(data['国家'] == '美国')]
#print(usa_gold)

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 获得金牌日期列表
date_str = list(set(usa_gold['日期']))
result_list = sorted(date_str, key=lambda x: datetime.strptime(x, "%Y/%m/%d"))
print(result_list)

#日期所对应的累加的银牌
def count_num(usa_three):
    group = usa_three.groupby('日期')  # 按日期分组
    s = group.size()  # 统计每天的奖牌个数
    #print(s)
    #print(len(s))
    sum = 0
    y = []
    # 统计每一天的奖牌总量
    for n in range(len(s)):
        sum = s[n] + sum
        y.append(sum)
    return y
# 日期所对应的累加的金牌、银牌、铜牌
data_gold = count_num(usa_gold)
data_silver = count_num(usa_silver)
data_bronze = count_num(usa_bronze)
print(data_gold)

'''
3. Pyecharts数据可视化
'''
# 3.1 绘制基础折线图
line = (
    Line()
    .add_xaxis(result_list)
    # 中国线条
    .add_yaxis(
        '中国', data_gold,
        label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title='中国金牌',
            pos_left='center',
        ),
        yaxis_opts=opts.AxisOpts(
            name='金牌/枚',
            is_scale=True,
            max_=40),
        legend_opts=opts.LegendOpts(is_show=False),
    ))
line.render("1基础折线图.html")

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7695795
  • 这篇博客你也可以参考下:pyecharts在数据可视化中的应用 (二)(pyecharts绘制树图、矩形树图、地理热力图、词云图、相关性矩阵等图)
  • 除此之外, 这篇博客: 使用pyecharts绘制系统依赖关系图中的 使用pyecharts绘制图形 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 现在已经有JSON文件了,工作完成了大部分。接下来就是编写代码绘制关系图,生成一个html文件。这个html文件是可交互的,可以移动鼠标或缩放。如果未来在这个页面增加其他一些检索或表格生成功能就更好了。可以作为未来继续尝试的点。这块没有什么算法逻辑。

    关键代码如下:

    json_file = "all_node_data.json"
    
    with open(json_file, 'r') as f:
        data_local = f.read()
        data = json.loads(data_local)
    
    nodes = [
        {
            # "x": node["x"],
            # "y": node["y"],
            "id": node["id"],
            "name": node["label"],
            "symbolSize": node["size"],
            "symbol": node["symbol"],
            # "itemStyle": {"normal": {"color": node["color"]}},
            "category": node["category"]
        }
        for node in data["nodes"]
    ]
    
    edges = [
        {"source": edge["sourceID"], "target": edge["targetID"], "value": edge["value"],
         "linestyle_opts": opts.LineStyleOpts(type_=edge["type_"]),
         } for
        edge in data["edges"]
    ]
    
    categories = data["categories"]
    
    (
        Graph(init_opts=opts.InitOpts(width="1600px", height="1200px"))
            .add(
            series_name="",
            nodes=nodes,
            links=edges,
            categories=categories,
            layout="circular",  # circular , force, none
            is_roam=True,
            is_focusnode=True,
            repulsion=100,
            is_rotate_label=True,
            label_opts=opts.LabelOpts(is_show=True, position="right"),
            linestyle_opts=opts.LineStyleOpts(width=1, curve=0.3, opacity=1.0),  # type_="solid"
            edge_symbol=["", "arrow"],  # 单向箭头
            edge_label=opts.LabelOpts(
                is_show=False, position="middle", formatter="{b}: {c} "  # 设置关系说明
            )
        )
            .set_global_opts(title_opts=opts.TitleOpts(title="系统数据关系图"),
                             legend_opts=opts.LegendOpts(orient="vertical", pos_left="2%", pos_top="20%"), )
            .render("relations-systems.html")
    )