【问题标题】:displaying month name chronologically for graphs in jupyter notebook在 jupyter notebook 中按时间顺序显示图表的月份名称
【发布时间】:2020-11-14 10:29:43
【问题描述】:

我正在 jupyter notebook 中创建一个图表,显示按月份排序的火灾因素。但是它是按字母顺序而不是按时间顺序对月份进行排序,我尝试过的方式似乎对图表没有任何影响。我对此非常陌生,因此将不胜感激!我还附上了图表的截图。沿着 x 轴,您可以看到月份。

import pandas as pd
import altair as alt
data = pd.read_csv('forestfires.csv')

new_order = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
data = data.reindex(new_order, axis=0)

alt.Chart(data).mark_circle().encode(
    alt.X('month:O',
          sort='ascending'), 
    alt.Y('FFMC:Q'),
    alt.Size('ISI:Q')
)

【问题讨论】:

    标签: python jupyter-notebook altair


    【解决方案1】:

    我认为 reinexing 会出现问题。 不要重新索引,它应该可以解决问题。

    data = data.set_index('new_order', inplace=True)
    

    【讨论】:

    • 嗯,现在它说 KeyError: "None of ['new_order'] are in the columns"
    • new_order 是您必须设置的列名或标题。就像你提到的标题“月”
    【解决方案2】:

    哦,我意识到它实际上可以非常简单地完成:

    alt.X('月:O', 标题='月', 排序 = [“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月” ]), ...等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多