日更2023年12月5-日签841

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python用matplotlib画2013-2023新闻平均数量情况条形图的代码: ```python import matplotlib.pyplot as plt import numpy as np # 列表 years = list(range(2013, 2024)) months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] year_months = [f"{year}-{month}" for year in years for month in months] # 模拟数据,每个随机一个数量 news_count = np.random.randint(0, 300, size=len(year_months)) # 计算每每个的平均数量 year_month_count = {} for year in years: for i, month in enumerate(months): count = np.mean(news_count[(year - 2013) * 12 + i:(year - 2013) * 12 + i + 1]) year_month_count[f"{year}-{month}"] = count # x轴和y轴数据 x = np.arange(len(year_months)) y = np.array([year_month_count[ym] for ym in year_months]) # 绘制条形图 plt.bar(x, y) # 设置x轴标签 plt.xticks(x[::12], years, rotation=45) # 设置y轴标签 plt.ylabel("Average Number of News") # 设置标题 plt.title("Average Number of News per Month from 2013 to 2023") # 显示图形 plt.show() ``` 解释一下代码: 1. 首先定义了列表 `years` 和 `months`,以及字符串的列表 `year_months`,用于后面生成每个的数据。 2. 接着使用 `numpy.random.randint()` 生成了模拟数据,每个随机一个数量。 3. 然后计算了每每个的平均数量,存储在字典 `year_month_count` 中。 4. 接着生成了x轴和y轴的数据,其中x轴是的序号,y轴是对应的平均新闻数量。 5. 使用 `matplotlib.pyplot.bar()` 函数绘制了条形图,并设置了x轴标签、y轴标签和标题。 6. 最后使用 `matplotlib.pyplot.show()` 函数显示图形。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值