Altair

  1. 常用画图
df5 = df.loc[(df["主管部门"] == '教育部'), :].groupby(['所在省市']).count().reset_index()
# df5.plot()
# set_index("time",inplace=True)
# df.plot()
alt.Chart(df5).mark_bar().encode(
    x='所在省市',
    y=alt.Y('序号'),
    color='所在省市',
).properties().interactive()


  1. 画图
# 画图1
# df
df.loc[:,'throughput'].plot()



# 画图2
ds = df.loc[:,'throughput']
ds
# 方法1
# df = ds.to_frame()  # 当Series的index也需要转变为DataFrame的一列时,这个方法转换会有一点问题
# df
# 方法2 OK
df = df.loc[:,'throughput'].reset_index()
# df
# 方法3 OK
df ={'time':ds.index,'throughput':ds.values}
df = pd.DataFrame(df)
df


alt.Chart(df).mark_line().encode(
    x='time',
#     y='Run Time',
    y=alt.Y('throughput')
#     color='Origin'
).properties(width=500,height=500).interactive()
  • tooltip 是为了添加数据提示的功能 (鼠标悬停在数据上时,会显示该数据的详细信息 );

  • color='country_id:N'的"N"改成"Q",则颜色为渐变色(要求原变量为数值);

    • Q:连续实值变量;
    • O:离散有序变量;
    • N:离散无序变量;
    • T:时间或日期变量;
  • Mark. 数据在图形中的表达形式。点、线、柱状还是圆圈?

  • Channels. 决定什么数据应该作为x轴,什么作为y轴;图形中数据标记的大小和颜色.

  • Encoding. 指定数据变量类型。日期变量、量化变量还是类别变量?

Method Description
mark_area() 直方图
mark_bar()
mark_circle() 圆点
mark_line() 直方图
mark_point() 圆圈
mark_rule() 直方图(接触边界)
mark_square() 正方形点
mark_text() 指定字符为图形(设置size和text)
mark_tick()

https://www.jianshu.com/p/056ee00f6a99
https://altair-viz.github.io/releases/changes.html?highlight=savechart

  1. 股票图画的方法

###############################################################
import pandas as pd
import numpy as np
import altair as alt

df = pd.DataFrame({
    'Date': pd.date_range('2019-01-01', freq='D', periods=20),
    'Adds': np.random.randint(0, 100, 20),
    'Deletes': np.random.randint(0, 100, 20),
    'Changes': np.random.randint(0, 100, 20),
})

chart = alt.Chart(df).transform_fold(
    ['Adds', 'Deletes', 'Changes']
).mark_bar().encode(
    x='yearmonthdate(Date):O',
    y='value:Q',
    color='key:N'
)
# chart

##############################################################
import pandas as pd
import numpy as np
import altair as alt

# Generate Random Data

df1=pd.DataFrame(10*np.random.rand(4,3),index=["A","B","C","D"],columns=["I","J","K"])
df2=pd.DataFrame(10*np.random.rand(4,3),index=["A","B","C","D"],columns=["I","J","K"])
df3=pd.DataFrame(10*np.random.rand(4,3),index=["A","B","C","D"],columns=["I","J","K"])

def prep_df(df, name):
    df = df.stack().reset_index()
    df.columns = ['c1', 'c2', 'values']
    df['DF'] = name
    return df

df1 = prep_df(df1, 'DF1')
df2 = prep_df(df2, 'DF2')
df3 = prep_df(df3, 'DF3')

df = pd.concat([df1, df2, df3])


# Plotting
chart = alt.Chart(df).mark_bar().encode(

    # tell Altair which field to group columns on
    x=alt.X('c2:N', title=None),

    # tell Altair which field to use as Y values and how to calculate
    y=alt.Y('sum(values):Q',
        axis=alt.Axis(
            grid=False,
            title=None)),

    # tell Altair which field to use to use as the set of columns to be  represented in each group
    column=alt.Column('c1:N', title=None),

    # tell Altair which field to use for color segmentation 
    color=alt.Color('DF:N',
            scale=alt.Scale(
                # make it look pretty with an enjoyable color pallet
                range=['#96ceb4', '#ffcc5c','#ff6f69'],
            ),
        ))\
    .configure_view(
        # remove grid lines around column clusters
        strokeOpacity=0    
    )

# chart.save('chart.pdf')
# chart.show()
chart
  1. 百分比柱状图

https://altair-viz.github.io/gallery/diverging_stacked_bar_chart.html?highlight=percentage


原文地址:https://www.cnblogs.com/amize/p/13974231.html