排序Altair列顺序

问题描述 投票:3回答:1

我是使用Altair的新手,并粗略地结合了herehere的一些代码,这些代码给出了下面的图,但我无法理解为什么我的列以非连续顺序显示。我的数据框如下:

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类)的任何想法都没有按索引顺序绘制列?如果我的月份数据是datetimeindex,有没有办法告诉Altair如下所示标记x轴(即mmm-yyyy)?

enter image description here

python-3.x pandas altair
1个回答
3
投票

您可以使用data type关键字指定type

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

您还可以使用Encoding Shorthands来定义数据类型

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

至于轴的格式,你可以在制作轴时传递format关键字date directive string

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')

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.