嗨,我有如下数据。
我需要动态编写代码来输入我的年份,并在月份和消息数量之间绘制图表。 2016 年在我的数据中只有 6 个月,并且不存在休息。
我尝试将年份设置为索引并尝试绘图。
dff[dff.index == 2015].plot(marker='*')
但是我做的情节并不是需要的。
最佳答案
使用boolean indexing
或query
用于过滤,然后 plot
带有参数x
和y
:
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/