首页 > 解决方案 > Seaborn lineplots:从数据框绘图仅绘制偶数图

问题描述

我正在尝试使用 seaborn 绘制多条线。csv格式的数据如下:

a,b,c,d,e,f,g
0,-4.0,18508,53.2972412601509,99.93867852215239,0.1465721586011575,0.500369685767098
0,-2.0,20362,58.63618038357426,90.46451019469569,0.2617993456058705,0.5574861367837338
0,-1.0,22816,65.70293152105052,67.08569676529204,0.4771268088645201,0.6816374455477443
0,-0.5,10827,31.178367793584055,31.1819714855128,0.6777462861074162,0.7475076104448686
0,0.0,-25249,-72.7092092380349,4.415146405028361,0.5997753552099969,0.5817141727824251
0,2.0,-37738,-108.67361631054541,0.07665184730952015,0.4580269332152725,0.5022461814914645
1,-4.0,18544,53.40090998099407,99.75471408860953,0.14901175733055483,0.5014787430683918
1,-2.0,21317,61.38628117260843,84.88425571056263,0.3209138230190862,0.590044226109514
1,-1.0,5797,16.693543742440824,28.575808676989116,0.6668365169814341,0.7193449786010732
1,-0.5,-24731,-71.21753153256925,4.568450099647402,0.604654265079733,0.5851232202852193
1,0.0,-33365,-96.08074641479007,1.5023762072665952,0.5152870968383397,0.5304837137310195
1,2.0,-37607,-108.29637735414389,0.10731258623332823,0.45988072279054165,0.5031446540880503
2,-4.0,18496,53.262685019869835,100.0,0.1457569408067051,0.5
2,-2.0,20089,57.85002591718021,91.85957381572895,0.2460291387477685,0.549075785582255
2,-1.0,22660,65.25370039739677,67.88287597731106,0.47028826718540884,0.6768315305754709
2,-0.5,9235,26.593906582963772,29.878890081250958,0.6782348818854779,0.7407386740138808
2,0.0,-26102,-75.16558198468006,4.231181971485513,0.5910931569156375,0.5757816794628429
2,2.0,-37999,-109.42521453665842,0.015330369461904032,0.45430028088552066,0.5004492362982929
3,-4.0,18496,53.262685019869835,100.0,0.1457569408067051,0.5
3,-2.0,20044,57.720440016126254,92.08952935765751,0.24339413629520643,0.5476894639556378
3,-1.0,22820,65.71445026781086,68.46543001686341,0.46652140422219357,0.6754861467483098
3,-0.5,9206,26.51039566895122,29.326996780622412,0.6824609868168487,0.7429825304376276
3,0.0,-26099,-75.15694292460981,4.215851602023609,0.5912367754786635,0.5758741009046174
3,2.0,-37999,-109.42521453665842,0.015330369461904032,0.45430028088552066,0.5004492362982929
4,-4.0,18805,54.15250820710706,98.42097194542389,0.16643301308091668,0.5095194085027727
4,-2.0,20224,58.238783620342105,91.16970718994328,0.2538732491015391,0.5532347504621071
4,-1.0,22569,64.99164890859875,77.78629464970106,0.3899189174598277,0.6317520381709689
4,-0.5,22906,65.96210332315844,64.87812356277787,0.4943158403127945,0.6922378444629342
4,0.0,16649,47.94390370327709,40.57948796565997,0.6398048308824541,0.7515189335246533
4,2.0,-37934,-109.23803490180268,0.030660738923808065,0.45523433885348985,0.5008984725965858
5,-4.0,18598,53.55641306225883,99.47876743829526,0.1526542475673964,0.5031423290203327
5,-2.0,20089,57.85002591718021,91.85957381572895,0.2460291387477685,0.549075785582255
5,-1.0,22427,64.58273339860622,78.85942051203433,0.3800821177203023,0.6258241949868218
5,-0.5,22997,66.22415481195645,65.81327609995401,0.48765933914808635,0.6887667674749598
5,0.0,17344,49.94528595288833,41.22336348305994,0.6388245977714251,0.7541371258509331
5,2.0,-37999,-109.42521453665842,0.015330369461904032,0.45430028088552066,0.5004492362982929

我使用以下代码进行绘图:

fig, ax = plt.subplots(nrows=1, ncols=5)
local_df = pd.read_csv('question.csv')

local_col_list = local_df.columns
for k, v in enumerate(list(local_col_list)):
    if k > 1:
        sns.lineplot(x='b', y=v, data=local_df, hue='a', ax=ax[k - 2])

输出图像 它显示色调变量 0、2、4、6。但是,从数据中可以看出,它应该有 0、1、2、3、4、5 和 6。

为什么会发生这种情况以及如何解决?

标签: pythonpandasmatplotlibplotseaborn

解决方案


请参阅这是 seaborn.lineplot 色调参数中的错误吗?

答案开始于:当色调可以转换为整数时,这是 seaborn 的一个已知错误。

因此,一种可能的解决方案是将列“a”更改为带有前置前缀的字符串,以阻止转换为整数。

local_df['a'] = 'C' + local_df.a.astype(str)

然后使用6种色调生成所有子图,在图例中标记为C0C5


推荐阅读