gpt4 book ai didi

python - 在 matplotlib 中使用 3 个变量和条件进行绘图

转载 作者:行者123 更新时间:2023-12-01 08:27:23 26 4
gpt4 key购买 nike

嗨,我有如下数据。

sample code

我需要动态编写代码来输入我的年份,并在月份和消息数量之间绘制图表。 2016 年在我的数据中只有 6 个月,并且不存在休息。

我尝试将年份设置为索引并尝试绘图。

dff[dff.index == 2015].plot(marker='*')

但是我做的情节并不是需要的。

最佳答案

使用boolean indexingquery用于过滤,然后 plot带有参数xy:

year = 2014

df[df['year'] == year].plot(x='month', y='Number of messages', marker='*')

或者:

df.query("year == @year").plot(x='month', y='Number of messages', marker='*')

另一种方法是绘制 Series 并按 Month 列进行索引:

df = df.set_index('month')
df.loc[df['year'] == year, 'Number of messages'].plot(marker='*')
<小时/>
df.set_index('month').query("year == @year")['Number of messages'].plot(marker='*')

关于python - 在 matplotlib 中使用 3 个变量和条件进行绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54157214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com