3

我是使用 Altair 的新手,并且粗略地组合了这里这里的一些代码,这给了我下面的图,但我无法弄清楚为什么我的列以非顺序顺序显示。我的数据框如下:

import pandas as pd

TimeMth = pd.DataFrame([['Jul-2011', '> 1.1 pu',  86],
                       ['Jul-2011', '< 0.94 pu',  18],
                       ['Aug-2011', '> 1.1 pu',  205],
                       ['Aug-2011', '< 0.94 pu', 510],
                       ['Sep-2011', '> 1.1 pu',   53],
                       ['Sep-2011', '< 0.94 pu', 140],
                       ['Oct-2011', '> 1.1 pu',   10],
                       ['Oct-2011', '< 0.94 pu',   0],
                       ['Nov-2011', '> 1.1 pu',   36],
                       ['Nov-2011', '< 0.94 pu',  13],
                       ['Dec-2011', '> 1.1 pu',    7],
                       ['Dec-2011', '< 0.94 pu',   0],
                       ['Jan-2012', '> 1.1 pu',   17],
                       ['Jan-2012', '< 0.94 pu',   0],
                       ['Feb-2012', '> 1.1 pu',    0],
                       ['Feb-2012', '< 0.94 pu',   0],
                       ['Mar-2012', '> 1.1 pu',   17],
                       ['Mar-2012', '< 0.94 pu',   1],
                       ['Apr-2012', '> 1.1 pu',   49],
                       ['Apr-2012', '< 0.94 pu',  79],
                       ['May-2012', '> 1.1 pu',    8],
                       ['May-2012', '< 0.94 pu',   0],
                       ['Jun-2012', '> 1.1 pu',   40],
                       ['Jun-2012', '< 0.94 pu',  12]],
                    columns=['Month','Voltage','No of Violations'])

然后我尝试绘制:

import altair as alt

chartTimeMth = alt.Chart(TimeMth).mark_bar().encode(
column=alt.Column('Month',
            axis=alt.Axis(axisWidth=1.0, 
               offset=-8.0, orient='bottom', labelAngle=-45, 
labelAlign='right'),
            scale=alt.Scale(padding=4.0)),
x=alt.X('Voltage',
       axis=False),
y=alt.Y('No of Violations', title='No. of voltage violations', 
axis=alt.Axis(grid=False)),
color=alt.Color('Voltage', scale=alt.Scale(range=['#96ceb4', '#ffcc5c']))
).configure_facet_cell(
strokeWidth=0.0,)\
.configure_scale(bandSize=12)\
.configure_legend(titleFontSize=12, offset=-60, orient='right')

chartTimeMth.display()

当我在列上使用sortField时,它接近我需要的但仍然不正确。我想这与我的列(月)数据是文本这一事实有关吗?关于为什么上面的代码(省略了sortField类)没有按索引顺序绘制列的任何想法?如果我的Month数据是datetimeindex,有没有办法告诉 Altair 像下面那样标记 x 轴(即 mmm-yyyy)?

在此处输入图像描述

4

1 回答 1

4

您可以使用关键字指定数据类型。type

column=alt.Column('Month',type='temporal', ....

您还可以使用编码速记来定义数据类型

column=alt.Column('Month:T', ...

至于轴的格式,您可以在制作轴时将format关键字传递给日期指令字符串

axis = alt.Axis(..., format='%b-%Y')

这是您之前的代码,其中进行了上述更改。

chartTimeMth = alt.Chart(TimeMth).mark_bar().encode(
    column=alt.Column(
        'Month:T',
        axis=alt.Axis(
            axisWidth=1.0, 
            offset=-8.0, 
            orient='bottom', 
            labelAngle=-45, 
            labelAlign='right', 
            format='%b-%Y'
        ),
        scale=alt.Scale(padding=4.0)
    ),
    x=alt.X('Voltage', axis=False),
    y=alt.Y(
        'No of Violations', 
        title='No. of voltage violations', 
        axis=alt.Axis(grid=False)),
        color=alt.Color(
            'Voltage', 
            scale=alt.Scale(range=['#96ceb4', '#ffcc5c'])
    )
).configure_facet_cell(strokeWidth=0.0,)\
 .configure_scale(bandSize=12)\
 .configure_legend(titleFontSize=12, offset=-60, orient='right')

在此处输入图像描述

于 2018-03-05T19:44:18.053 回答