Python中的多级分组X轴

时间:2019-03-06 01:27:42

标签: python pandas matplotlib seaborn bokeh

我有一个如下的数据集。我想像在JMP中使用具有多个类别和按图例的分组X轴来绘制可变性图。下面是来自JMP的数据集和图解示例。是否有Pythonic解决方案来绘制此类数据?我正在寻找使用任何python绘图库-bokeh,matplotlib,seaborn等的解决方案。

请注意,最底端的X类别必须水平显示,而子类别必须垂直。如果该绘图可以灵活地处理各种X轴类别(意味着将来会添加更多),那就太好了。

这是一个转贴,因为它与我之前关于Python中的分组X轴Var图的帖子稍有不同。

下面的数据集和示例JMP图:

Sample DataSet

VarPlot Example - JMP

1 个答案:

答案 0 :(得分:0)

这是我使用Altair绘图库生成此组合条形图的方式。有两个限制

  1. TEST_NAME列从每一行到下一行永远是相同的,因此它不会显示在图上
  2. Date列从每一行到下一行始终是相同的,因此它不会显示在图上

进口

import altair as alt
import pandas as pd

Create样本数据

data = """
Name,Numbers,Date,TEST_NAME,Label,Data
ABC,404,201905,101,MEDIAN,0.745313
ABC,404,201905,102,NINETYFIVEPERC,1.03828
ABC,406,201905,103,MEDIAN,0.698438
ABC,406,201905,104,NINETYFIVEPERC,0.874219
ABC,408,201905,105,MEDIAN,0.721785
DEF,408,201905,106,NINETYFIVEPERC,1.05
DEF,411,201905,107,MEDIAN,0.7277345
DEF,411,201905,108,NINETYFIVEPERC,1.0083995
DEF,414,201905,109,MEDIAN,0.757031
DEF,414,201905,110,NINETYFIVEPERC,1.05
GHI,415,201905,111,MEDIAN,0.733594
GHI,415,201905,112,NINETYFIVEPERC,0.932813
GHI,441,201905,113,MEDIAN,0.745313
GHI,441,201905,114,NINETYFIVEPERC,0.96738305
GHI,498,201905,115,MEDIAN,0.721875
"""
df = pd.read_csv(pd.compat.StringIO(data), sep=',')

显示数据

print(df)

   Name  Numbers    Date  TEST_NAME           Label      Data
0   ABC      404  201905        101          MEDIAN  0.745313
1   ABC      404  201905        102  NINETYFIVEPERC  1.038280
2   ABC      406  201905        103          MEDIAN  0.698438
3   ABC      406  201905        104  NINETYFIVEPERC  0.874219
4   ABC      408  201905        105          MEDIAN  0.721785
5   DEF      408  201905        106  NINETYFIVEPERC  1.050000
6   DEF      411  201905        107          MEDIAN  0.727735
7   DEF      411  201905        108  NINETYFIVEPERC  1.008400
8   DEF      414  201905        109          MEDIAN  0.757031
9   DEF      414  201905        110  NINETYFIVEPERC  1.050000
10  GHI      415  201905        111          MEDIAN  0.733594
11  GHI      415  201905        112  NINETYFIVEPERC  0.932813
12  GHI      441  201905        113          MEDIAN  0.745313
13  GHI      441  201905        114  NINETYFIVEPERC  0.967383
14  GHI      498  201905        115          MEDIAN  0.721875

生成分组的条形图

alt.Chart(df).mark_circle(size=100).encode(
    x='Numbers:O',
    y='Data',
    color='Label',
    column='Name'
)

这是结果图 Chart