绘图空白是否因ca_i(y值)量级差异过大导致缩放问题?
解决空白绘图问题:处理数值量级差异过大的数据可视化
你遇到的空白绘图确实是ca_i数值量级差异过大导致的——数据范围从0到0.00186,后续数值甚至降到了1e-54,线性坐标轴下绝大多数极小值会被压缩到基线附近,看起来就是空白。另外你的t数组存在乱序(比如15.0之后出现13.0),这也会让折线图逻辑混乱。
以下是两种解决思路:
1. 使用对数坐标轴(最直接有效)
对数坐标轴能拉开极小值的显示空间,不过要注意处理ca_i中的0值(对数无法处理0),可以将0替换为一个略小于最小非零值的数(比如1e-55)。同时对t和ca_i按时间排序,让折线逻辑更清晰:
import numpy as np import matplotlib.pyplot as plt ca_i = np.array([0.0,0.0018600003438189192,0.0002590223247024505,4.2933247094276643e-05,1.01517329122632e-06,2.2000181987561245e-08,3.1829754066400425e-09,9.251619028714474e-12,1.3077449672199555e-12,3.6241917918406e-15,1.8421082461592794e-13,5.06566621963767e-16,9.840901934293446e-18,3.649674529308416e-21,9.64374923981959e-24,2.5298107231198704e-26,2.3519077811337855e-32,4.429035906892334e-34,1.1414882447414905e-36,4.016168548404073e-40,1.9241495055371506e-44,3.595405959272557e-46,9.170496181343466e-49,1.2525305730959068e-49,5.939334658820937e-54]) t = np.array([0.0,0.8,2.0,3.0,5.0,7.0,8.0,11.0,12.0,15.0,13.0,16.0,18.0,22.0,25.0,28.0,35.0,37.0,40.0,44.0,49.0,51.0,54.0,55.0,60.0]) # 处理0值 ca_i[ca_i == 0] = 1e-55 # 按时间排序数据 sorted_indices = np.argsort(t) t_sorted = t[sorted_indices] ca_i_sorted = ca_i[sorted_indices] plt.plot(t_sorted, ca_i_sorted) plt.yscale('log') # 设置y轴为对数刻度 plt.xlabel('时间t') plt.ylabel('ca_i') plt.title('原始数据对数轴可视化') plt.show()
2. 过滤极小值或分区间可视化
如果坚持用线性坐标轴,可以过滤掉小于某个阈值的数值(比如1e-10),或者将数据分成前几个大值区间和后续小值区间分别绘图,避免大值把小值压缩:
import numpy as np import matplotlib.pyplot as plt ca_i = np.array([0.0,0.0018600003438189192,0.0002590223247024505,4.2933247094276643e-05,1.01517329122632e-06,2.2000181987561245e-08,3.1829754066400425e-09,9.251619028714474e-12,1.3077449672199555e-12,3.6241917918406e-15,1.8421082461592794e-13,5.06566621963767e-16,9.840901934293446e-18,3.649674529308416e-21,9.64374923981959e-24,2.5298107231198704e-26,2.3519077811337855e-32,4.429035906892334e-34,1.1414882447414905e-36,4.016168548404073e-40,1.9241495055371506e-44,3.595405959272557e-46,9.170496181343466e-49,1.2525305730959068e-49,5.939334658820937e-54]) t = np.array([0.0,0.8,2.0,3.0,5.0,7.0,8.0,11.0,12.0,15.0,13.0,16.0,18.0,22.0,25.0,28.0,35.0,37.0,40.0,44.0,49.0,51.0,54.0,55.0,60.0]) # 过滤极小值 threshold = 1e-10 mask = ca_i >= threshold t_filtered = t[mask] ca_i_filtered = ca_i[mask] plt.plot(t_filtered, ca_i_filtered) plt.xlabel('时间t') plt.ylabel('ca_i') plt.title('过滤极小值后的线性轴可视化') plt.show()
内容的提问来源于stack exchange,提问作者Jesus Vargas Jimenez
相关产品推荐
相关产品推荐

