You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何用Matplotlib绘制2015-2022年标签提及量趋势图表

用Matplotlib实现技术标签年度提及趋势可视化

没问题,我来帮你搞定这个Matplotlib可视化需求!下面是完整的实现步骤和代码,你可以直接套用,只需要替换成你2015-2022年的真实数据就行。

第一步:整理数据结构

首先,我们需要把每年的标签数据整理成嵌套字典的形式,方便后续遍历绘图。假设你每年的标签列表一致,示例结构如下(你需要把对应年份的真实数值填进去):

# 示例数据,替换成你自己的2015-2022年真实数据
tag_data = {
    2015: {"PHP":282, "JavaScript":273, "HTML":229, "HTML5":227, "MySQL":218, "CSS":217, "CSS3":203, "PHP5":184, "jQuery":168, "1С":168},
    2016: {"PHP":265, "JavaScript":280, "HTML":232, "HTML5":225, "MySQL":210, "CSS":212, "CSS3":200, "PHP5":178, "jQuery":160, "1С":162},
    2017: {"PHP":250, "JavaScript":295, "HTML":228, "HTML5":220, "MySQL":205, "CSS":208, "CSS3":195, "PHP5":170, "jQuery":152, "1С":158},
    2018: {"PHP":235, "JavaScript":310, "HTML":220, "HTML5":215, "MySQL":200, "CSS":202, "CSS3":190, "PHP5":162, "jQuery":145, "1С":155},
    2019: {"PHP":220, "JavaScript":325, "HTML":215, "HTML5":210, "MySQL":195, "CSS":198, "CSS3":185, "PHP5":155, "jQuery":138, "1С":150},
    2020: {"PHP":210, "JavaScript":340, "HTML":210, "HTML5":205, "MySQL":190, "CSS":192, "CSS3":180, "PHP5":148, "jQuery":130, "1С":145},
    2021: {"PHP":200, "JavaScript":355, "HTML":205, "HTML5":200, "MySQL":185, "CSS":188, "CSS3":175, "PHP5":140, "jQuery":122, "1С":140},
    2022: {"PHP":190, "JavaScript":370, "HTML":200, "HTML5":195, "MySQL":180, "CSS":182, "CSS3":170, "PHP5":132, "jQuery":115, "1С":135}
}

第二步:定义自定义颜色映射

按照你的要求给指定标签设置颜色,其他标签可以用默认灰色兜底:

color_mapping = {
    "PHP": "#2ecc71",    # 绿色
    "CSS3": "#3498db",   # 蓝色
    "JavaScript": "#f1c40f",  # 黄色
    "HTML": "#e67e22",   # 橙色
    "HTML5": "#e74c3c",  # 红色
    "MySQL": "#9b59b6",  # 紫色
    "CSS": "#1abc9c",    # 青色
    "PHP5": "#34495e",   # 深灰色
    "jQuery": "#95a5a6", # 浅灰色
    "1С": "#f39c12"      # 橙黄色
}

第三步:绘制折线图(推荐用于年度趋势)

折线图最适合展示年度数据的变化趋势,代码如下:

import matplotlib.pyplot as plt

# 设置画布大小
plt.figure(figsize=(12, 7))

# 获取排序后的年份列表
years = sorted(tag_data.keys())
# 获取所有标签(假设每年标签一致)
all_tags = list(tag_data[2015].keys())

# 遍历每个标签,绘制年度折线
for tag in all_tags:
    # 提取该标签每年的提及数量
    yearly_counts = [tag_data[year][tag] for year in years]
    # 绘制折线,使用自定义颜色,添加标记点
    plt.plot(years, yearly_counts, label=tag, color=color_mapping.get(tag, "#808080"), marker='o', linewidth=2)

# 设置坐标轴标签和标题
plt.xlabel("年份", fontsize=12)
plt.ylabel("提及数量", fontsize=12)
plt.title("2015-2022年技术标签提及数量趋势", fontsize=14, pad=20)

# 左上角添加图例,调整位置避免遮挡图表
plt.legend(loc="upper left", fontsize=10, bbox_to_anchor=(1, 1))

# 添加网格线,增强可读性
plt.grid(True, alpha=0.3)

# 自动调整布局,防止图例被截断
plt.tight_layout()

# 显示图表
plt.show()

可选:绘制分组条形图

如果你更倾向于用条形图对比每年的标签数据,可以用下面的代码:

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(14, 8))

years = sorted(tag_data.keys())
all_tags = list(tag_data[2015].keys())
bar_width = 0.08  # 每个标签条形的宽度
x = np.arange(len(years))  # 年份的X轴位置

# 遍历每个标签绘制条形
for i, tag in enumerate(all_tags):
    yearly_counts = [tag_data[year][tag] for year in years]
    plt.bar(x + i*bar_width, yearly_counts, width=bar_width, label=tag, color=color_mapping.get(tag, "#808080"))

# 设置X轴刻度为年份
plt.xticks(x + bar_width*(len(all_tags)-1)/2, years)

plt.xlabel("年份", fontsize=12)
plt.ylabel("提及数量", fontsize=12)
plt.title("2015-2022年技术标签提及数量对比", fontsize=14, pad=20)
plt.legend(loc="upper left", fontsize=10, bbox_to_anchor=(1, 1))
plt.grid(axis='y', alpha=0.3)
plt.tight_layout()
plt.show()

注意事项

  • 请确保你已经安装了Matplotlib:pip install matplotlib
  • 如果你的每年标签列表不一致,需要稍微调整代码来处理缺失标签的情况(比如用get方法设置默认值0)
  • 颜色代码可以根据你的喜好自由替换,用十六进制颜色码或者Matplotlib内置颜色名都可以

内容的提问来源于stack exchange,提问作者Proger228

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 17:50:22