Seaborn箱线图与散点图叠加后X轴刻度标签错位问题
Seaborn箱线图与散点图组合后X轴刻度标签错位问题
问题详情
尝试用Seaborn绘制箱线图+散点图的组合图表时,出现X轴刻度标签错位:红色散点对应df_concat最后一行数据,却无法和箱线图的X轴标签对齐;箱线图使用的是df_concat.iloc[:-5,:]的数据。
数据展示
df_concat数据如下:
1 2 3 4 5 6 7 8 9 10 11 12 0 704.8 785.4 743.2 945.2 650.5 775.8 841.8 561.2 548.5 690.2 785.8 822.5 1 833.8 827.0 734.7 819.1 668.4 612.5 745.4 472.1 368.6 644.8 893.0 652.5 2 776.6 762.1 825.0 873.5 954.3 705.4 612.1 388.8 444.8 660.5 786.5 693.3 3 942.0 914.3 862.8 928.7 1136.5 645.8 638.2 552.3 514.7 736.5 850.9 851.9 4 829.1 738.3 733.4 823.4 599.0 635.3 603.4 586.0 419.5 662.2 732.7 818.5 5 845.7 798.6 924.4 762.7 831.8 1033.7 626.9 426.1 461.8 725.9 713.2 763.7 6 932.7 885.9 880.1 1028.0 855.3 758.8 573.9 476.8 455.4 537.7 574.1 800.5 7 827.6 932.2 898.1 871.2 939.7 836.2 631.6 455.7 548.4 710.1 803.6 790.8 8 777.7 839.3 680.6 995.1 807.1 656.2 592.8 536.2 474.6 694.2 678.0 768.1 9 891.6 772.7 932.5 961.0 947.1 633.9 617.6 551.8 547.1 548.4 714.6 857.6 10 685.5 828.3 669.1 890.0 795.0 611.8 536.2 447.2 471.4 529.9 807.5 818.6 11 751.7 760.7 780.8 823.1 874.8 693.2 532.2 543.7 411.6 747.7 744.8 774.1 12 784.8 686.4 787.8 876.3 713.4 726.0 537.1 485.5 475.1 547.5 764.2 784.8 13 854.9 858.4 1009.9 930.2 757.3 552.8 479.1 532.3 529.3 648.0 759.9 780.7 14 749.1 763.7 842.9 825.6 674.0 634.3 443.8 420.0 402.2 586.4 664.3 886.0 15 924.9 938.0 1016.6 957.2 914.8 848.2 539.3 434.9 447.4 569.3 681.2 923.6 16 816.9 990.5 958.6 994.2 763.1 742.7 714.1 593.5 425.7 814.2 599.2 745.2 17 798.4 696.2 843.6 898.5 880.9 758.0 555.7 459.3 509.2 586.3 885.1 834.6 18 742.0 978.1 912.6 1173.1 1081.3 831.5 596.2 536.1 476.1 638.7 926.8 798.6 19 835.7 777.0 909.1 848.8 775.0 634.2 585.0 564.0 518.9 724.7 657.8 848.7 20 815.2 825.9 886.9 906.8 899.3 715.2 540.5 472.0 519.7 664.4 752.5 744.3 21 843.2 818.8 830.9 910.5 880.5 887.3 562.8 555.5 490.4 567.0 843.8 695.8 22 706.6 740.0 550.6 726.7 869.0 514.6 690.1 490.0 441.0 590.3 785.0 848.6 23 772.8 810.4 835.3 730.8 720.3 490.0 748.9 505.5 462.7 545.0 654.9 825.2 24 923.1 902.4 874.9 911.3 844.3 609.6 554.7 616.5 558.8 631.4 735.5 721.2 25 780.9 875.5 854.5 884.1 805.4 803.7 552.6 610.1 421.7 694.2 730.5 823.8 26 787.4 787.8 824.8 861.4 919.0 693.1 652.8 627.4 555.2 756.1 750.3 744.5 27 857.4 856.0 943.0 796.8 824.3 688.4 664.2 460.9 588.9 658.2 657.9 806.5 28 783.4 910.7 866.2 850.2 798.1 621.5 446.9 518.8 504.1 626.2 662.4 894.6 29 769.9 792.2 820.6 1027.8 1007.9 706.0 677.0 539.4 349.1 569.3 779.7 778.4 30 276.9 415.7 435.1 538.7 561.7 583.4 580.5 503.8 364.2 353.4 320.0 245.1 31 292.6 220.4 259.4 171.4 188.3 196.0 175.2 146.9 233.5 280.3 305.3 270.4 32 280.9 241.2 280.6 263.2 204.7 164.7 120.1 111.2 144.8 225.9 264.0 267.2 33 322.9 267.5 324.9 301.3 274.2 249.1 195.2 188.8 252.7 300.9 329.5 321.0 34 545.3 496.3 533.2 450.0 492.8 394.6 362.0 330.6 289.4 367.8 423.5 528.5
错误表现
图表中红色散点的X轴位置与箱线图的X轴标签错位,散点整体偏移,无法和对应列的箱线图对齐。
问题代码
import seaborn as sns import matplotlib.pyplot as plt # make the boxplot of data for rows 1-29 only df_concat = df_concat.reset_index(drop=True) plt.figure(figsize=(12,6)) g = sns.boxplot(data=df_concat.iloc[:-5,:], orient='v', color='y',whis=100) g.set_xticks(range(0,12)) g.set_xticklabels(['1','2','3','4','5','6','7','8','9','10','11','12']) #add scatterplot BUDGET data...using only last row in df_concat g = sns.scatterplot(x=df_concat.iloc[-1,:].index, y=df_concat.iloc[-1,:], color='red',marker='o',label='2023 Budget') g.set_xticklabels(['1','2','3','4','5','6','7','8','9','10','11','12']) plt.show()
解决方案
问题原因
散点图的x参数传入的是列名(如'1','2'...),这些是字符串类型,Seaborn会将其作为分类变量重新分配X轴位置;而箱线图的X轴是基于列的索引位置(0到11),两者的X轴坐标体系不匹配,导致错位。
修正代码
使用列的索引位置作为散点图的X轴坐标,确保和箱线图的X轴位置一致:
import seaborn as sns import matplotlib.pyplot as plt df_concat = df_concat.reset_index(drop=True) plt.figure(figsize=(12,6)) # 绘制箱线图 g = sns.boxplot(data=df_concat.iloc[:-5,:], orient='v', color='y', whis=100) g.set_xticks(range(0,12)) g.set_xticklabels(['1','2','3','4','5','6','7','8','9','10','11','12']) # 绘制散点图:用列的索引位置作为x值 last_row = df_concat.iloc[-1,:] sns.scatterplot(x=range(len(last_row)), y=last_row.values, color='red', marker='o', label='2023 Budget', ax=g) plt.legend() plt.show()
修正说明
- 散点图的
x参数使用range(len(last_row)),生成和箱线图一致的X轴位置(0到11),确保散点和对应列的箱线图对齐。 - 使用
ax=g指定散点图绘制在同一个坐标轴上,避免重新生成X轴。 - 保留原有的刻度标签设置,确保显示正确的列名。
内容的提问来源于stack exchange,提问作者user2100039
相关产品推荐
相关产品推荐

