箱线图中添加的垂直线与 x 轴不匹配
Posted
技术标签:
【中文标题】箱线图中添加的垂直线与 x 轴不匹配【英文标题】:Vertical lines added in a boxplot do not match the x-axis 【发布时间】:2022-01-23 01:46:50 【问题描述】:我想每隔 24 X 间隔在箱线图 seaborn 上添加垂直线。
我的功能:
def BoxPlot(data,x_attrib, y_attrib, showfliers=False, orient='h', *args):
sns.set(style="whitegrid", palette="Set3", context='talk', color_codes=False) #défaut
fig, ax = plt.subplots(1,1,figsize=(20,5))
ax = sns.boxplot(x=x_attrib, y=y_attrib, data=data, showfliers = showfliers, orient=orient, palette='Dark2', ax=ax) #palette="Set2")
list_diff_hours = np.sort(data[x_attrib].astype(int).unique())
print(list_diff_hours)
for i in list_diff_hours:
if (i % 24 == 0):
print(f"""Add line x=i""")
ax.axvline(x = i, color = 'b', label = '12')
ax.set(title=args[0])
ax.set(xlabel=args[1]) # idem ax.set(xlabel=None)
ax.set(ylabel=args[2])
#ax.set(xticklabels=[]) # remove labels from x axis
ax.tick_params(bottom=False) # remove the ticks
# Show the plot
plt.show()
我绘制了 n 条垂直线:
我的问题是 data['x_attrib'] 不连续:
DIFF_HOURS DIFF_TEMP
0 0.0 0.050886
1 1.0 0.660698
2 2.0 1.656014
3 3.0 2.543857
4 4.0 3.071813
... ... ...
627770 336.0 -7.524095
627771 339.0 -4.180723
627772 342.0 -8.724546
627773 345.0 -10.176562
627774 348.0 -8.047641
627775 rows × 2 columns
函数执行:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
108 109 110 111 112 113 114 115 116 117 118 119 123 126 129 132 135 138
141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192
195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246
249 252 255 258 261 264 267 270 273 276 279 282 285 288 291 294 297 300
303 306 309 312 315 318 321 324 327 330 333 336 339 342 345 348 351 354
357]
Add line x=0
Add line x=24
Add line x=48
Add line x=72
Add line x=96
Add line x=144
Add line x=168
Add line x=192
Add line x=216
Add line x=240
Add line x=264
Add line x=288
Add line x=312
Add line x=336
我不应该在最后一个 X 箱线图值之后有线条。 我不明白。
调用函数:
path = '/xxx'
data2boxplot = pd.read_csv(os.path.join(path, 'merged2_limited.csv'), sep=';')
BoxPlot(data2boxplot,
'DIFF_HOURS',
'DIFF_TEMP',
False,
'v',
'',
'',
'')
另外如何在每行的 x 轴上放置标签?
样本数据
Data Download - .csvDIFF_HOURS,DIFF_TEMP
0.0,0.0508860903513017
1.0,0.6606980150309028
2.0,1.6560138915914993
3.0,2.543856665504201
4.0,3.071813181144403
5.0,1.8228499885861
6.0,-1.0648394742460994
7.0,-2.488141151430004
8.0,-3.367705894714298
9.0,-3.672485488901201
10.0,-2.965489148744801
11.0,-3.054565515742897
12.0,-2.0708812793241016
13.0,-0.7613814777512964
14.0,0.2020813798845964
15.0,0.3136517751847023
16.0,0.7578654959031006
17.0,1.796600153839602
18.0,1.3360555256778
19.0,0.5195588315561004
20.0,-0.1587534779601007
21.0,-0.3501035240801009
22.0,-0.7686862284051017
23.0,-0.4259603228679971
24.0,0.636230830620697
25.0,0.8782201238649989
26.0,0.8752404883571039
27.0,0.9896782693150996
28.0,1.4808193880072018
29.0,1.359074500951401
30.0,0.1090185745514986
31.0,-0.1983728888428011
32.0,-1.2747932083237998
33.0,0.3264950295098004
34.0,1.5323613807559011
35.0,0.6369611820368988
36.0,-0.1668353314066024
37.0,-0.4585072776349008
38.0,-0.7295583937719954
39.0,-0.6062539959064956
40.0,0.5665020058765045
41.0,1.0616182067213984
42.0,1.8872550413563007
43.0,1.915382734552498
44.0,1.0471410630142015
45.0,0.8662996625566991
46.0,1.2746546032826949
47.0,1.0679496294792017
48.0,0.977680775822602
0.0,0.9481029455852016
1.0,2.1050969347580017
2.0,3.2760407873637014
3.0,4.344428893295703
4.0,4.901783099360504
5.0,3.696887332163403
6.0,0.1667104898304003
7.0,-2.371896438511804
8.0,-4.2627128900091975
9.0,-5.002792798693502
10.0,-5.2455918994013
11.0,-3.780849468299799
12.0,-1.0769181236025034
13.0,0.222992037187403
14.0,0.3435438092772962
15.0,0.8102643110400045
16.0,1.4313979808253023
17.0,2.3581243639427005
18.0,2.477428665879401
19.0,1.8329917112030003
20.0,1.533774692003398
21.0,1.5107766037682993
22.0,0.9821263738569002
23.0,0.9859066714612011
24.0,1.5215579940998971
25.0,1.4286036097316988
26.0,1.4520266307993026
27.0,1.5522584243467
28.0,2.2495906999692004
29.0,2.2265263619725
30.0,0.736071352020101
31.0,0.0544039530452984
32.0,-1.6178014448367009
33.0,-0.6747283502522983
34.0,0.0745035742702988
35.0,-1.3139941917547022
36.0,-2.166329466286101
37.0,-1.8654762187255969
38.0,-0.9528454300361986
39.0,0.2444552716616002
40.0,0.3638052974010044
41.0,0.6938993599382997
42.0,1.6715946531027015
43.0,2.2912391124936
44.0,1.9091871864761991
45.0,1.6538121525740976
46.0,1.871403680868596
47.0,1.8755397730923036
48.0,1.683526230989102
49.0,3.9257324576676993
50.0,4.2606635532902
51.0,3.547404399117198
52.0,3.5442721882085984
53.0,2.0273757348208967
54.0,0.9674114895829008
55.0,0.4955297870016011
56.0,0.2259864728987999
57.0,0.291238747112402
58.0,0.0919498398644016
59.0,-0.2344818522044001
60.0,-0.6529058591203025
61.0,-1.3347805797921986
62.0,-1.085802560345197
63.0,-1.1364933815099008
64.0,1.6930235479636018
65.0,3.1144047645493984
66.0,3.0742791475726
67.0,2.5129622649698007
68.0,3.9519612202572993
69.0,3.424352795620901
70.0,3.6924282332803
71.0,3.9638111547679
72.0,3.928898849589199
73.0,4.3853295811086
74.0,4.497082965096396
75.0,4.996047910204702
76.0,5.192069417326497
77.0,3.967237413111
78.0,0.9978052732892984
79.0,-0.9260451604602976
80.0,-1.6127062604781983
81.0,-2.3512202960404984
82.0,-3.023051506510697
83.0,-3.860725480091496
84.0,-4.3561004082297
85.0,-1.9432349456607056
86.0,-0.3325606119583
87.0,0.3767153699817029
88.0,2.5923976945151
89.0,2.6999498567612967
90.0,3.6577365981003993
91.0,3.819267929856804
92.0,4.2311134009189
93.0,3.8719392524307
94.0,4.112123045242598
95.0,3.5670466428843
96.0,3.707961014061997
97.0,3.3575633147774973
98.0,2.609527103988804
99.0,2.6393175591536
100.0,2.841210257546596
101.0,1.9100548192853035
102.0,-0.9261718909340964
1.0,-1.3538390543185983
2.0,-0.4993995934072011
3.0,0.3545493177938006
4.0,0.8099865903292027
5.0,-0.0506968341322
6.0,-0.6804032287808006
7.0,-0.2082854185232037
8.0,0.1630975835187023
9.0,1.3459815168519995
10.0,0.8188580444433953
11.0,1.2689348507551017
12.0,2.5538855454437983
13.0,3.0004117809216027
14.0,2.5512122750664936
15.0,2.405389571822603
16.0,2.191629812401601
17.0,2.2951491862325
18.0,1.2686938279479032
19.0,-2.0014185726731
20.0,-2.6544771602099004
21.0,-2.633052784155602
22.0,-3.092243465084401
23.0,-2.937052744408298
24.0,-2.0957011820857008
25.0,-2.3233501028976
26.0,-2.3927692215934973
27.0,-2.512657772196601
28.0,-1.7964380456293014
29.0,-1.7847067206928993
30.0,-0.2526341077528009
31.0,1.5703263586214966
32.0,1.4113320887482992
33.0,2.9538918436626
34.0,4.0837719685110985
35.0,3.1727485835231017
36.0,1.9205350337043043
37.0,0.8241595096379015
38.0,0.0327764865730984
39.0,0.4453579476395007
40.0,0.0302729352231025
41.0,-0.5416959697029
42.0,-1.340051189759798
43.0,-3.1358167635487004
44.0,-3.659751635387998
45.0,-3.0855913898442995
46.0,-3.200745120168304
47.0,-2.7721319693662
48.0,-2.7245163840206
49.0,-0.3698256650070988
50.0,0.3255698682053989
51.0,0.171604221326298
52.0,0.5237634028585987
53.0,-0.9555386881921032
54.0,-1.1099254073318008
55.0,-1.0289337447369995
56.0,0.0710421633454991
57.0,1.8551764454929007
58.0,3.3021008765933004
59.0,4.493103892326103
60.0,4.459707548881899
61.0,3.8100320431610015
62.0,2.70771353528
63.0,1.811856686758496
64.0,3.1732643515642
65.0,2.7837551373168985
66.0,0.6481714824083014
67.0,-1.5360733736842
68.0,0.2581648129808975
69.0,0.0466600478800991
70.0,0.3247838415439972
71.0,0.480818944133901
72.0,0.3697571191573985
73.0,0.7756447728369977
74.0,0.6264768722090963
75.0,0.5598319716264015
76.0,0.5475730464813999
77.0,-0.5839476160479968
78.0,-1.5219718889375002
79.0,-0.8823618347315012
80.0,0.3616040380187009
81.0,1.4167763945443037
82.0,2.2441204477612047
83.0,2.2022495423586013
84.0,1.816157556111598
85.0,2.9791984000074905
86.0,3.256299154099601
87.0,2.2553796714922
88.0,3.0085705007726986
89.0,1.5769306424971958
90.0,0.8768902546595996
91.0,-0.7117263891153982
92.0,-0.3782886285789999
93.0,-0.1900556523495993
94.0,0.2031325340318979
95.0,-0.4954602383545996
96.0,-0.6076415962878023
97.0,-0.9117851715344008
98.0,-1.4458622670138972
99.0,-1.2197078521700997
100.0,-0.8343423884402021
101.0,-1.5738029409072
102.0,-2.5020333517593976
103.0,-1.4883967580332005
104.0,-0.5267064568487996
105.0,0.3055101751269973
106.0,0.8410028019703972
107.0,0.0563780428751954
108.0,0.3206620074736008
109.0,2.1680835552511013
110.0,0.7204345807845058
111.0,1.0092727386892049
112.0,1.5859000383682975
113.0,2.400175965303301
114.0,1.1687751483999025
115.0,-1.4135078863699988
116.0,-1.6965739903897976
117.0,-1.8758467646699977
118.0,-2.216906436912499
119.0,-2.2461669675342013
123.0,-3.5438322357964016
126.0,-2.634330549200701
129.0,3.1095237582866986
132.0,7.574655737394298
135.0,2.0845096505009018
138.0,-0.7407272283859001
141.0,-1.3589556692384015
144.0,-3.1358191915115974
147.0,-0.3873845281203962
150.0,-0.5938085200652985
153.0,0.3747979208253973
156.0,3.5077837934555016
159.0,0.8830075640103026
162.0,-0.6721012052772011
165.0,-3.1283774896485
168.0,-1.567625477271296
171.0,-0.4029477457177997
174.0,-1.2982829966962974
177.0,-0.4720358919086998
180.0,5.048419824699401
189.0,-2.1594860545862
195.0,0.9763107957826024
198.0,-1.0529880563177976
201.0,-2.214585153295502
204.0,-2.6355890021463964
207.0,-2.185078189591998
210.0,-1.0333854681407004
213.0,-1.3780712108308997
216.0,-2.042565978695201
219.0,-1.1856517905460002
222.0,-2.3636010060555
225.0,-2.2247376450717984
228.0,-1.9685004380312991
231.0,0.9751352549206942
234.0,-0.0051869832737025
237.0,-1.179880232604102
240.0,-0.1703304982377993
243.0,-0.1837155289762009
246.0,-1.6687987582940984
249.0,-0.7126965251013004
252.0,0.6958547475151988
255.0,-0.1953486997794016
258.0,-1.170362566589997
261.0,-2.4442778503078024
264.0,-2.869701790766201
267.0,-4.622253130391896
270.0,-5.890969728006404
273.0,-4.098677950872801
276.0,-1.6764041775382026
279.0,-4.262179550511107
282.0,-6.3015133817497
285.0,-6.005815502043498
288.0,-5.378570257987403
291.0,-5.047018048669899
294.0,-5.857756250553898
297.0,-1.8460496728989
300.0,0.9079803925058024
303.0,-0.6062438711246969
306.0,-4.256089629439798
309.0,-5.3899103924226965
312.0,-5.186848294790604
315.0,-4.623624887780702
318.0,-5.099925375859198
321.0,-0.0612520652145001
324.0,2.2867713220248014
327.0,0.1717713032915995
330.0,-3.8374781189160982
333.0,-3.4730529203284988
336.0,-1.0676838067522991
0.0,-0.1459566586928993
1.0,0.0614772121906028
2.0,0.7363874732168014
3.0,1.2031075586225022
4.0,1.9538959368440023
5.0,0.8693056244497015
6.0,-2.007499321760097
7.0,-4.195665168729299
8.0,-3.9699415437067
9.0,-5.135802190955001
10.0,-5.702742608368793
11.0,-4.250843026306498
12.0,-2.340146933246796
13.0,-0.8793467560561012
14.0,-0.2161077788790031
15.0,-0.1077915636018964
16.0,0.768099693175202
17.0,1.7220874184740045
18.0,2.063656233830901
19.0,1.678385038792399
20.0,1.0817611710609008
21.0,0.2852614193162992
22.0,-0.587591679850398
23.0,-1.3162566908359956
24.0,-1.2437537679398003
25.0,-1.0559501484568017
26.0,-0.1837786148366014
27.0,0.9858895387046972
28.0,1.2866163848955026
29.0,0.9351783620958044
30.0,-0.7142171180706995
31.0,-1.9599658748459028
32.0,-1.8583128090592
33.0,-1.3252073519866985
34.0,-1.1712412188778032
35.0,-1.5320732524449987
36.0,-2.1684233260155
37.0,-2.106434459053702
38.0,-0.9668425795828028
39.0,0.1805581214584961
40.0,0.7766958703514001
41.0,1.182157960361501
42.0,1.1584910459410978
43.0,1.0284982444010993
44.0,0.8924115143780007
45.0,0.8486554579783991
46.0,1.2583065174074015
47.0,1.5739775389096025
48.0,1.5734835048864009
0.0,0.3952015147116015
1.0,0.7377887043529014
2.0,1.6047851576544012
3.0,2.325849166181804
4.0,3.379004675116004
5.0,2.208471823802501
6.0,-1.0446257933893968
7.0,-3.3921082965297984
8.0,-3.258990330240202
9.0,-4.071408465300499
10.0,-4.410703748548696
11.0,-3.3792716456841947
12.0,-1.0952086898309972
13.0,-0.7647998780504963
14.0,0.6018091511193973
15.0,0.8874504236594021
16.0,1.310425725629603
17.0,2.089458136459601
18.0,2.27098162978
19.0,1.858444908298999
20.0,1.3866667310050005
21.0,1.3794258588033017
22.0,0.972807690249102
23.0,0.6574421069312031
24.0,0.9866939031244968
25.0,1.0956438204335974
26.0,1.3806613275869992
27.0,1.9521652799865985
28.0,1.8899318101797
29.0,1.4500142465100048
30.0,-0.2740230887754009
31.0,-1.7510969061237027
32.0,-1.8175492591397997
33.0,-1.3322954320065
34.0,-0.9622027189693014
35.0,-0.8674047081823986
36.0,-1.1298251419121996
37.0,-1.3675841468255
38.0,-0.6140622192948015
39.0,-0.0154552517453048
40.0,0.2721064369936989
41.0,0.7982662860750018
42.0,1.479662074118398
43.0,1.9217716271265
44.0,1.9205495499703
45.0,1.898866014190201
46.0,2.305668091427499
47.0,2.5914265141508004
48.0,2.5974296685145006
49.0,2.6649327029623
50.0,2.270721723059601
51.0,2.5149380945251
52.0,2.6214812157153027
53.0,1.8966545214528985
54.0,-0.6647528753270997
55.0,-2.7921635236217037
56.0,-3.8710270692794992
57.0,-3.808989980522398
58.0,-3.262861612133398
59.0,-0.7398446516122057
60.0,0.204042234428293
61.0,0.4463703942047985
62.0,0.1832234428295009
63.0,0.1808788257785991
64.0,0.8871658942551015
65.0,1.8454354759957
66.0,2.092024220061102
67.0,1.9511863337449
68.0,2.0203161107831
69.0,1.4341807348758984
70.0,1.0024733104432997
71.0,0.9848073331137002
72.0,0.4397358522800019
73.0,0.1296178859979022
74.0,-0.3781480265349017
75.0,0.0285207640216995
76.0,0.2803727253850994
77.0,0.0184907562226008
78.0,-1.8255554825655016
79.0,-3.1180966174389013
80.0,-3.6258135772260016
81.0,-3.2916458427972017
82.0,-2.157294786692105
83.0,-0.6099603908906985
84.0,-0.2297082827665022
85.0,-1.2482605184650026
86.0,-1.1461407119243034
87.0,-0.0476702521096044
88.0,0.4045311718668003
89.0,0.2523017364159017
90.0,1.7461879839430985
91.0,1.770547192748101
92.0,1.2773135103536042
93.0,1.6557247868518026
94.0,1.891346210256504
95.0,2.055787014716998
96.0,2.3351465799899955
97.0,2.4125520618094995
98.0,2.443035799030397
99.0,2.8541213190802
100.0,2.9177416026372005
101.0,1.9326161696030013
102.0,0.3172392739786041
1.0,0.9286207747280032
2.0,1.4556391588261022
3.0,1.7251112868489038
4.0,2.4270607023222013
5.0,1.0544704591357998
6.0,-0.7244050077092972
7.0,-1.7045914328533982
8.0,-0.8271018539506017
9.0,-1.4253452668274988
10.0,-0.0992236674891984
11.0,0.0317953069144039
12.0,0.5567005317060065
13.0,0.6302894578386002
14.0,1.7345861284194015
15.0,1.5273450298563027
16.0,1.1883081312150026
17.0,0.8205596935315036
18.0,-0.0621186467461996
19.0,-1.8017738943387016
20.0,-1.8959905338838008
22.0,-2.0441933534308987
23.0,-2.5221742430796965
24.0,-2.4083803504139
25.0,-2.4123860668522035
26.0,-2.2711722441486017
27.0,-1.8669197175185024
28.0,-2.0613691466828
29.0,-2.4587163646538954
30.0,-2.1408043913283983
31.0,-1.0414160900877008
32.0,-0.0807733319557009
33.0,0.6667732260052013
34.0,0.4397750109072973
35.0,1.010480445987202
36.0,1.1869082935332038
37.0,0.9824616187468004
38.0,1.2575810270158991
39.0,1.4847380458479975
40.0,0.9454837917915988
41.0,0.3867869360883027
42.0,-0.5455990241636002
43.0,-2.0888443946824005
44.0,-2.0588567791498997
45.0,-1.7263700277462988
46.0,-0.9624458594607006
47.0,-0.4815377390727989
48.0,-0.3028111564252001
49.0,-0.0528160457777993
50.0,-0.3474254662428002
51.0,0.0411866400990987
52.0,0.116750011163802
53.0,-0.4715022553509023
54.0,-1.0698599924728995
55.0,-1.3100769882233043
56.0,-1.0039878890521976
57.0,-0.6796406908819996
58.0,-0.3198871811542005
59.0,1.0759608773936975
60.0,0.6039382992445965
61.0,1.070072583463798
62.0,0.7061093415399995
63.0,0.0255818223689985
64.0,-0.1668997526619975
65.0,-0.2475401636691004
66.0,-1.3404254515168963
67.0,-2.962668413566103
68.0,-2.3753294689608992
69.0,-2.062904120630801
70.0,-2.360149357273098
71.0,-1.9720304702255007
72.0,-2.1313856696208973
73.0,-2.0325266621062
74.0,-2.3964146958604005
75.0,-1.7929453087177976
76.0,-1.4057418128401018
77.0,-1.6322615854636986
78.0,-2.5601119069714
79.0,-2.9508767745448985
80.0,-1.9094673832161
81.0,-1.154792223779502
82.0,-0.3147406190340049
83.0,0.8393132229029021
84.0,1.0416951255198017
85.0,-0.1339773809966047
86.0,-0.8980408132717983
87.0,-1.0174019175763078
88.0,-2.1795762379056995
89.0,-3.3623235838404995
90.0,-3.0897519949006025
91.0,-3.552197347747597
92.0,-3.2953707612886984
93.0,-2.5085119755689966
94.0,-2.2353894096587967
95.0,-2.107161120921801
96.0,-1.9797086154986017
97.0,-2.3700249951895
98.0,-2.705106623875704
99.0,-2.6108714654967
100.0,-2.6905427108710995
101.0,-3.5249146035029995
103.0,-1.9197423357166985
104.0,-0.8764955688158977
105.0,0.1088363146952993
106.0,1.220153899475502
107.0,1.8617352471708983
108.0,1.9844364332081028
109.0,1.7541343606392985
110.0,0.9440491625158992
111.0,0.379277599632001
112.0,-0.5835490334422992
113.0,-1.5060875426241012
114.0,-2.5448722782830018
115.0,-3.472255493484901
116.0,-3.3124482897155016
117.0,-3.459294499290096
118.0,-3.515504545250401
119.0,-3.2495471637119984
123.0,-3.2534629456914
126.0,-3.907920517118999
129.0,-0.1655151665237006
132.0,1.2390220596054
135.0,0.0651535942233998
138.0,-3.3824740664604
141.0,-3.1727102259943014
144.0,-0.8581871221005991
147.0,-0.6236684467602025
150.0,-1.507933450372601
153.0,0.948615952051
156.0,0.644392381802497
159.0,0.7671592440587993
162.0,-0.6088484719264997
165.0,-0.3190825353973956
168.0,0.9979311032384004
171.0,2.3789051756223003
174.0,-0.2279707531639978
177.0,-2.6147172681903967
180.0,-5.687688081022799
183.0,-4.264250165085496
186.0,-4.086051033316799
189.0,-4.436630448942804
192.0,-1.7462869872545994
195.0,-2.8048389937807947
198.0,-4.598961445100598
201.0,-2.607349136140701
204.0,-4.131877326799
207.0,-2.5662234451172985
210.0,-4.141615945916296
213.0,-4.983479457423299
216.0,-5.799790165241003
219.0,-2.6692345564201965
222.0,-5.703517930211003
225.0,-5.433548056198004
228.0,1.048105662357198
231.0,3.0394180613788
234.0,5.239499224335604
237.0,1.6021590460481008
240.0,0.460489530186301
243.0,-0.4751959422441985
246.0,0.2786735868794015
249.0,6.7205369509407005
252.0,9.463506137181104
255.0,6.2749624728099
258.0,0.0256556625770976
261.0,-1.3705103244422998
264.0,-1.2190090278872994
267.0,0.490321614015901
270.0,0.3373327286749017
273.0,4.689931087450901
276.0,6.927270767326
279.0,6.028349390376397
282.0,0.3743446848848997
285.0,0.5891991768293003
288.0,1.777312492458602
291.0,2.0698136326590006
294.0,0.3598981930848026
297.0,4.702606757084404
300.0,7.159011662508007
303.0,4.974587873807798
306.0,1.399013308151602
309.0,1.4672505352598009
312.0,2.8387602182602976
315.0,3.0050100242769027
318.0,2.8036972988185984
321.0,4.793382051483499
324.0,5.869674302284802
327.0,6.7424981370394965
330.0,4.3204317035522015
333.0,3.6240385820357983
336.0,2.7771457770068007
0.0,-0.4036096780988992
1.0,0.0277671912413985
2.0,-0.4234571785524004
3.0,-0.0971067952294006
4.0,-0.1646330549983972
5.0,-0.9706288617761024
6.0,-2.7976802205024
7.0,-3.846093669865201
8.0,-4.589214919053899
9.0,-4.4296833539210985
10.0,-4.201867499845696
11.0,-1.398403561674403
12.0,-1.452894224313603
13.0,-0.3229703505441037
14.0,0.1172833264055981
15.0,0.2565145610499968
16.0,0.4709111631953036
17.0,1.127451986661498
18.0,1.662596177933203
19.0,1.3699571558042969
20.0,0.919289657257
21.0,-0.1878526042362018
22.0,-0.8743987829260007
23.0,-1.088425539215102
24.0,-1.600459778034498
25.0,-1.9557307205871977
26.0,-2.2852944864269027
27.0,-1.7763967064922994
28.0,-1.4601081813600023
29.0,-0.9107629876002968
30.0,-0.5913007793492007
31.0,-1.157834656194801
32.0,-1.4260668044445026
33.0,-1.5714559977263995
34.0,-1.4619625227327049
35.0,-0.7362994575282968
36.0,-0.5934204266385024
37.0,-1.3755304011086054
38.0,-0.5095352952061987
39.0,0.4141898571318947
40.0,1.0833501193837023
41.0,1.0811141387735006
42.0,1.9707157509842967
43.0,1.9441506861576023
44.0,1.843768622137404
45.0,2.0763030252168018
46.0,1.7778919032969045
47.0,1.4515748491071996
48.0,1.2100787629862957
0.0,1.039198546382103
1.0,1.4060808363307018
2.0,1.2689761918906015
3.0,1.6876551845844998
4.0,1.6567531759614018
5.0,0.8315558640748009
6.0,-1.6632729713663004
7.0,-3.685577899310904
8.0,-4.4257732877616975
9.0,-4.345232570209198
10.0,-3.831917536454597
11.0,-0.7886086284461058
12.0,-0.8121139303394003
13.0,-0.3913496407733987
14.0,-0.6892303091573986
15.0,-1.0272519655630994
16.0,-0.4866409788546982
17.0,0.5831217976239991
18.0,1.0879605728028032
19.0,1.0761344058160986
20.0,1.2949918206431017
21.0,0.8263308071788984
22.0,0.4509620870230009
23.0,0.541095995065799
24.0,0.2204233793434014
25.0,0.2021884463187007
26.0,-0.0521023002186993
27.0,0.3222848149612999
28.0,0.5988001178717965
29.0,0.5970919553363032
30.0,-1.3425320890568029
31.0,-2.6359871384341003
32.0,-2.5580507241316006
33.0,-3.5633986213244
34.0,-4.172359626263905
35.0,-3.1606135602152
36.0,-2.3180192125940025
37.0,-2.5213492943348044
38.0,-1.918410819749397
39.0,-0.8531897131421076
40.0,-0.5131729684236959
41.0,-0.4155793809809012
42.0,0.8921616284075995
43.0,1.2624801338692038
44.0,1.2725731869880048
45.0,1.561841588998604
46.0,1.8675404175618016
47.0,1.9417824416954976
48.0,2.0213581949100963
49.0,1.8232968737440025
50.0,1.6713795104187952
51.0,1.8833890731101024
52.0,1.9565898514737008
53.0,1.1712294374992993
54.0,-0.1848679982981984
55.0,-0.8269120977115989
56.0,-0.8271794916981996
57.0,-0.4298079884441002
58.0,0.2187221265945034
59.0,0.6038884129936974
60.0,0.7946596286585006
61.0,0.8687738375358975
62.0,0.5787591096178986
63.0,0.8845790077969014
64.0,1.0074479356562025
65.0,0.8680342676363964
66.0,1.2120202574065004
67.0,1.5658757422290996
68.0,1.9121373179227
69.0,1.908818082190504
70.0,2.0731325646921985
71.0,2.424235933292902
72.0,2.544312097389497
73.0,2.708851395836703
74.0,2.9001366105491013
75.0,3.2442337580382983
76.0,4.176603280932
77.0,3.486173517551997
78.0,0.8829834333408009
79.0,-0.2867270473338976
80.0,-0.7668052940533023
81.0,-0.3804444401301019
82.0,0.0427422968580017
83.0,-0.0952547562601004
84.0,0.1752267015672011
85.0,0.7686474641468024
86.0,1.1573631118137016
87.0,1.3493051461173984
88.0,1.3965456120345967
89.0,1.503269395314199
90.0,1.8008556843895995
91.0,1.8432560217484983
92.0,2.0040832172458
93.0,2.271802308074101
94.0,3.3265095272156024
95.0,3.607149763048501
96.0,4.4018567495279015
97.0,3.4989871984528023
98.0,3.8458616350106993
99.0,4.627192144447296
100.0,4.996864456083099
101.0,4.384017223420397
102.0,1.2118606158107
1.0,0.1584059146812002
2.0,-0.0422710810052002
3.0,0.3121108946491979
4.0,0.3012659783945004
5.0,-0.372622993161201
6.0,-1.1419045323212984
7.0,-1.3523209566689012
8.0,-0.7453918917859994
9.0,-0.8361894989194987
10.0,-0.4748565995503995
11.0,1.027038445555398
12.0,0.6066669279261987
13.0,0.9075001795461972
14.0,0.5802417617599005
15.0,-0.0198066372031995
16.0,-0.0739965215054994
17.0,-0.0043928435502991
18.0,-1.052988263129297
19.0,-2.9327984110567016
20.0,-2.440980978558102
21.0,-2.5442235520301004
22.0,-2.5275701725229
23.0,-2.2135611948637006
24.0,-2.443283897940997
25.0,-2.5233110840841
26.0,-2.7378275952768014
27.0,-2.1185105849668986
28.0,-1.7998148052238
29.0,-2.005732912447197
30.0,-2.3299193541721017
31.0,-1.8717215505432
32.0,-1.0783941852008
33.0,-0.2626428431053007
34.0,1.2087419169238984
35.0,3.0890077542994017
36.0,3.0859831744521955
37.0,1.2694850671616962
38.0,0.096136345338003
39.0,-0.0058360744168055
40.0,-0.8579317665897008
41.0,-2.101499869479401
42.0,-2.7672424595562006
43.0,-3.341124128384596
44.0,-2.990159901943496
45.0,-2.3638649157862983
46.0,-2.191319097103697
47.0,-2.093379077197003
48.0,-1.6717392131374034
49.0,-1.865123911872299
50.0,-2.144995005626204
51.0,-1.8727376962377
52.0,-1.9267143900937
53.0,-2.7733154019798008
54.0,-2.608001932101597
55.0,-0.9201489836826012
56.0,0.2194995176034027
57.0,1.1449074267136972
58.0,2.1520961437641013
59.0,2.9678628542444976
60.0,3.2597757356275
61.0,3.006455525826801
62.0,2.101336059987201
63.0,1.4663466101524032
64.0,0.4175499859934035
65.0,-0.7651207815850007
66.0,-1.9766218176389003
67.0,-2.6792939536505003
68.0,-2.4901232401959037
69.0,-2.793574254287595
70.0,-2.9241403607009
71.0,-2.9214961037634986
72.0,-2.831098003033801
73.0,-2.5985901088248973
74.0,-2.3238314975130017
75.0,-2.054434352479003
76.0,-1.0949389811626986
77.0,-1.499543122395103
78.0,-2.6922230139208985
79.0,-1.7820011152210995
80.0,-0.6775540239893019
81.0,0.5661510729621
82.0,1.7090062695916008
83.0,1.7098229496096984
84.0,1.8350596714612983
85.0,1.7781301423100049
86.0,1.5270209395636982
87.0,0.5677495714081005
88.0,-0.5779070147848024
89.0,-1.5985390300141018
90.0,-2.8843436606799
91.0,-3.6531419710743016
92.0,-3.1368570585342006
93.0,-2.7872482219278005
94.0,-1.5948362562698968
95.0,-1.2337338495071002
96.0,-0.4814594087474973
97.0,-1.3796559764352985
98.0,-1.016758405524001
99.0,-0.3336501742188034
100.0,-0.0124908490349007
101.0,-0.4591254955074042
102.0,-1.7046340180923991
103.0,-2.0385279823156988
104.0,-0.8221126867023009
105.0,0.3123749831240978
106.0,0.8702267688102978
107.0,-0.8009710795719016
108.0,1.1325529428576004
109.0,3.1904686768132966
110.0,2.9642923972938
111.0,2.5648023393206003
112.0,2.6259412575977024
113.0,1.6444638366968007
114.0,-0.3928903586623989
115.0,-2.208837528829001
116.0,-1.7033542518184994
117.0,-1.1759223247819968
118.0,-0.7268371013310002
119.0,-0.179290956812899
123.0,1.6995964011442015
126.0,-0.9564751403393998
129.0,2.4491633220296047
132.0,1.804410111773301
135.0,1.1901569362663054
138.0,-2.7884915258654
141.0,-3.793874546199704
144.0,0.3191043697955997
147.0,-0.8414748552238969
150.0,-2.3995930327342023
153.0,-1.3478701973471985
156.0,0.5398694831379984
159.0,2.7227897166740966
162.0,-0.9947717465670962
165.0,-1.2100169251434991
168.0,-1.6093793226776043
171.0,1.3643049672529024
174.0,-2.121432282991403
177.0,-7.338827111269102
180.0,-8.059594368203001
183.0,-6.703410630180098
186.0,2.0881489524899024
189.0,0.6911943010101993
192.0,1.2003755601024009
195.0,0.8651961787615008
198.0,0.9361071303318004
201.0,5.758962330985899
204.0,4.665616167039302
207.0,0.9754068890282994
210.0,-1.7039656750764998
213.0,-2.5131268943295986
216.0,-1.929969327456
219.0,-0.1395818071250989
222.0,-0.2319067961754992
225.0,1.0158127425640018
228.0,2.7657979196659
231.0,1.8335651681357
234.0,-1.1727980557473003
237.0,-1.4302294406147986
240.0,-0.7764675365631
243.0,-0.2553325558395993
246.0,-1.6008935979452978
249.0,1.2320391546181035
252.0,2.064484340181803
255.0,0.2556331969106012
258.0,-2.5915933888595006
261.0,-2.1948203358837013
264.0,-0.1607927039965027
【问题讨论】:
【参考方案1】: 如果你做ax.get_xticks()
,你会看到只有198个xticks
。唯一的DIFF_HOURS
从 0 到 357,但箱线图是由 xtick 位置绘制的,而不是由 xtick 标签绘制的。
xticks
的数量将对应唯一hours
的数量,因此可以为hours
创建一个布尔掩码,用于获取对应的xticks
,然后用于绘制垂直线.
请参阅此answer 并查看关于条形图和直方图 的部分,这对于箱形图也是相同的。此外,如答案所示,使用 ax.vlines
消除了使用 for 循环的需要。
ax.vlines(x=vlines, ymin=ymin, ymax=ymax, color='b', zorder=1)
设置垂直线的zorder
。
在python 3.10
、pandas 1.3.5
、matplotlib 3.5.1
、seaborn 0.11.2
中测试
更新功能
def BoxPlot(data, x_attrib, y_attrib, showfliers=False, orient='h', *args):
sns.set(style="whitegrid", palette="Set3", context='talk', color_codes=False) #défaut
fig, ax = plt.subplots(figsize=(25, 5))
ax = sns.boxplot(x=x_attrib, y=y_attrib, data=data, showfliers=showfliers, orient=orient, palette='Dark2', ax=ax) #palette="Set2")
# get the xticks
xticks = ax.get_xticks()
# get the unique values for DIFF_HOURS
hours = np.sort(df.DIFF_HOURS.unique().astype(int))
# create a Boolean mask
mask = hours % 24 == 0
# Use mask to get the xticks corresponding to True
vlines = xticks[mask]
# get y min and max from the plot
ymin, ymax = ax.get_ylim()
# set the y margins to 0
ax.margins(y=0)
# add the vertical lines
ax.vlines(x=vlines, ymin=ymin, ymax=ymax, color='b')
ax.set(title=args[0])
ax.set(xlabel=args[1]) # idem ax.set(xlabel=None)
ax.set(ylabel=args[2])
# remove the xticks
ax.tick_params(bottom=False)
# remove xticks and format labels - if desired
# ax.tick_params(bottom=False, axis='x', rotation=90, labelsize=8)
# use the masked ticks and labels to only label at the vertical lines
ax.set_xticks(ticks=vlines, labels=hours[mask], fontsize=10)
# Show the plot
plt.show()
函数调用
path = 'd:/data/merged2_limited.csv'
df = pd.read_csv(path, sep=';')
BoxPlot(df,
'DIFF_HOURS',
'DIFF_TEMP',
False,
'v',
'',
'',
'')
与所有 xticklabels 交替使用
【讨论】:
非常感谢!在 Colab 上安装最后一个 Matplotlib 版本后它工作正常(以前的版本我遇到了问题)。你节省了我的一天! @Theo75 不客气。我很高兴这对你有用。以上是关于箱线图中添加的垂直线与 x 轴不匹配的主要内容,如果未能解决你的问题,请参考以下文章