xaxis python_Python中的分组Xaxis可变性图

您可以尝试以下代码,您需要修改plot的xlim和ylim参数以适合您的实际数据:import pandas as pd

import matplotlib.pyplot as plt

from itertools import groupby

import numpy as np

%matplotlib inline

df = pd.DataFrame({'Name':['John']*2+['David']*2+['Mike']*2+['Albert']*2+['King']*2+['Brown']*2,

'TEST_Name':['Class A']*6+['Class B']*6,

'Label':['Median','NINETYFIVEPERC']*6,

'Data':[.54,.62,.55,.62,.55,.67,.58,1.05,.54,.60,.54,.60]})

df = df.set_index(['TEST_Name','Name','Label'])['Data'].unstack()

def add_line(ax, xpos, ypos):

line = plt.Line2D([xpos, xpos], [ypos + .1, ypos],

transform=ax.transAxes, color='gray')

line.set_clip_on(False)

ax.add_line(line)

def label_len(my_index,level):

labels = my_index.get_level_values(level)

return [(k, sum(1 for i in g)) for k,g in groupby(labels)]

def label_group_bar_table(ax, df):

ypos = -.1

scale = 1./df.index.size

for level in range(df.index.nlevels)[::-1]:

pos = 0

for label, rpos in label_len(df.index,level):

lxpos = (pos + .5 * rpos)*scale

ax.text(lxpos, ypos, label, ha='center', transform=ax.transAxes)

add_line(ax, pos*scale, ypos)

pos += rpos

add_line(ax, pos*scale , ypos)

ypos -= .1

ax = df.plot(marker='o', linestyle='none', xlim=(-.5,5.5), ylim=(.3,1.1))

#Below 2 lines remove default labels

ax.set_xticklabels('')

ax.set_xlabel('')

label_group_bar_table(ax, df)

输出图表:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值