Matplotlib .plot()未显示全部数据点问题求助
Matplotlib折线图重合数据仅显示单条的问题排查与解决
我一眼就发现问题所在啦——你看你的group_data里,Group4、Group5、Group6的数值完全一模一样!从代码里的列表就能看出来,这三个组的10个年度数据点没有任何区别,所以用plot()绘制的时候,三条折线完全重叠在一起,视觉上就只剩一条了,这可不是Matplotlib的bug哦。
问题原因
你的group_data中索引为3、4、5的三个子列表(对应Group4、5、6)的数值完全相同,例如:
group_data[3] == group_data[4] == group_data[5] # 结果是True
当多条折线的x、y坐标完全一致时,它们会精确重合,肉眼无法区分,看起来就像只显示了一条。
解决办法
根据你的需求,有几种实用的方案:
方案1:为重合数据添加微小偏移(适合需要区分趋势的场景)
如果数据确实是独立的组(只是数值碰巧完全一致),可以给每组数据加一个极小的偏移量,让折线稍微分开一点:
import matplotlib.pyplot as plt from matplotlib.ticker import StrMethodFormatter group_data = [[0.019661816751867872, 0.02043028685790527, 0.02023318156674184, 0.017430534194606787, 0.019330407797782608, 0.01969285217721039, 0.020037761795104064, 0.02195322742933805, 0.02104150568005187, 0.023173911874613543], [0.046087298466378296, 0.04628085390260173, 0.04816816305689119, 0.05218907002973444, 0.060561320045386814, 0.0625292296024774, 0.0604625274209567, 0.05977172708885976, 0.05898549566113317, 0.06442723875581365], [0.015257569799449469, 0.01667778519012675, 0.017148206459299604, 0.015841279606274992, 0.01711575004442986, 0.01660873412121595, 0.01645803092974778, 0.019442835218668374, 0.02056187659321236, 0.019746216092695647], [0.1048368069209595, 0.1054869913275517, 0.10197438406876304, 0.0988584708978434, 0.0944373812355603, 0.09515262592428743, 0.09366234843441344, 0.09388053785407202, 0.09037455478874115, 0.0906121461408178], [0.1048368069209595, 0.1054869913275517, 0.10197438406876304, 0.0988584708978434, 0.0944373812355603, 0.09515262592428743, 0.09366234843441344, 0.09388053785407202, 0.09037455478874115, 0.0906121461408178], [0.1048368069209595, 0.1054869913275517, 0.10197438406876304, 0.0988584708978434, 0.0944373812355603, 0.09515262592428743, 0.09366234843441344, 0.09388053785407202, 0.09037455478874115, 0.0906121461408178]] years = ['2008','2009','2010','2011','2012','2013','2014','2015','2016','2017'] # 定义微小偏移量,根据y轴比例调整 offset = 0.0005 plt.xlabel('Year') plt.ylabel('Percentage') plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.0%}')) plt.plot(years, group_data[0], label='Group 1') plt.plot(years, group_data[1], label='Group 2') plt.plot(years, group_data[2], label='Group 3') # 给后三组添加偏移 plt.plot(years, [x + offset for x in group_data[3]], label='Group 4') plt.plot(years, [x + 2*offset for x in group_data[4]], label='Group 5') plt.plot(years, [x + 3*offset for x in group_data[5]], label='Group 6') plt.legend() plt.show()
方案2:使用不同的线条样式/标记(适合强调组的独立性)
如果不需要改变数据本身,可以用不同的线条样式(实线、虚线、点线)或标记点来区分重合的折线,即使重叠也能通过样式识别:
import matplotlib.pyplot as plt from matplotlib.ticker import StrMethodFormatter group_data = [[0.019661816751867872, 0.02043028685790527, 0.02023318156674184, 0.017430534194606787, 0.019330407797782608, 0.01969285217721039, 0.020037761795104064, 0.02195322742933805, 0.02104150568005187, 0.023173911874613543], [0.046087298466378296, 0.04628085390260173, 0.04816816305689119, 0.05218907002973444, 0.060561320045386814, 0.0625292296024774, 0.0604625274209567, 0.05977172708885976, 0.05898549566113317, 0.06442723875581365], [0.015257569799449469, 0.01667778519012675, 0.017148206459299604, 0.015841279606274992, 0.01711575004442986, 0.01660873412121595, 0.01645803092974778, 0.019442835218668374, 0.02056187659321236, 0.019746216092695647], [0.1048368069209595, 0.1054869913275517, 0.10197438406876304, 0.0988584708978434, 0.0944373812355603, 0.09515262592428743, 0.09366234843441344, 0.09388053785407202, 0.09037455478874115, 0.0906121461408178], [0.1048368069209595, 0.1054869913275517, 0.10197438406876304, 0.0988584708978434, 0.0944373812355603, 0.09515262592428743, 0.09366234843441344, 0.09388053785407202, 0.09037455478874115, 0.0906121461408178], [0.1048368069209595, 0.1054869913275517, 0.10197438406876304, 0.0988584708978434, 0.0944373812355603, 0.09515262592428743, 0.09366234843441344, 0.09388053785407202, 0.09037455478874115, 0.0906121461408178]] years = ['2008','2009','2010','2011','2012','2013','2014','2015','2016','2017'] plt.xlabel('Year') plt.ylabel('Percentage') plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.0%}')) plt.plot(years, group_data[0], label='Group 1') plt.plot(years, group_data[1], label='Group 2') plt.plot(years, group_data[2], label='Group 3') # 用不同的线条样式和标记 plt.plot(years, group_data[3], label='Group 4', linestyle='--', marker='o') plt.plot(years, group_data[4], label='Group 5', linestyle=':', marker='s') plt.plot(years, group_data[5], label='Group 6', linestyle='-.', marker='^') plt.legend() plt.show()
方案3:合并图例说明(如果数据确实是重复的)
如果这三组数据本来就是重复的(比如是同一数据的多次复制),可以在图例里说明它们是重合的,比如把这三个组的标签改成Group 4/5/6 (重合),只绘制一次即可:
import matplotlib.pyplot as plt from matplotlib.ticker import StrMethodFormatter group_data = [[0.019661816751867872, 0.02043028685790527, 0.02023318156674184, 0.017430534194606787, 0.019330407797782608, 0.01969285217721039, 0.020037761795104064, 0.02195322742933805, 0.02104150568005187, 0.023173911874613543], [0.046087298466378296, 0.04628085390260173, 0.04816816305689119, 0.05218907002973444, 0.060561320045386814, 0.0625292296024774, 0.0604625274209567, 0.05977172708885976, 0.05898549566113317, 0.06442723875581365], [0.015257569799449469, 0.01667778519012675, 0.017148206459299604, 0.015841279606274992, 0.01711575004442986, 0.01660873412121595, 0.01645803092974778, 0.019442835218668374, 0.02056187659321236, 0
相关产品推荐
相关产品推荐

