如何自定义scp.plot_multiple优化拉曼光谱分析绘图?
解决方案
a) 将所有谱线设置为实线
在scp.plot_multiple中添加linestyles参数,指定所有线为实线(可用字符串'solid'或短横线'-'):
_ = scp.plot_multiple(method="pen", style="sans", datasets=data, labels=labels, legend="best", title="Laser Test", xlim=(200.0, 1800.0), ylim=(0.0, 300.0), linestyles='solid') # 也可以传列表:linestyles=['-', '-', '-']
如果需要单独指定某条线的样式,直接传对应长度的列表即可,比如linestyles=['-', '--', ':'],但这里全设为实线,传单个值更简洁。
b) scp.plot_multiple的更多自定义选项
谱线颜色自定义
使用colors参数指定每条线的颜色,支持颜色名称、十六进制码或RGB值:
_ = scp.plot_multiple(method="pen", style="sans", datasets=data, labels=labels, legend="best", title="Laser Test", xlim=(200.0, 1800.0), ylim=(0.0, 300.0), linestyles='solid', colors=['red', '#2E8B57', 'darkblue']) # 三条线分别用红、海绿、深蓝
标题及文本元素自定义
Spectrochempy绘图基于Matplotlib,你可以获取plot_multiple返回的轴对象,用Matplotlib原生API修改样式:
ax = scp.plot_multiple(method="pen", style="sans", datasets=data, labels=labels, legend="best", title="Laser Test", xlim=(200.0, 1800.0), ylim=(0.0, 300.0), linestyles='solid') # 修改标题颜色和字体大小 ax.set_title("Laser Test", color='darkred', fontsize=14) # 修改X轴标签颜色 ax.set_xlabel("Raman Shift", color='darkblue') # 统一调整所有谱线宽度 for line in ax.get_lines(): line.set_linewidth(2)
其他常用自定义参数:
linewidths:设置线宽(单个值或列表)marker:添加数据点标记(如'o'、's')legend_kwargs:传递图例自定义参数(如{'fontsize': 12, 'frameon': False})
c) 1D绘图实现类似Matlab hold on的效果
Python中只需在同一个Matplotlib轴对象上多次调用plot方法即可,步骤如下:
import matplotlib.pyplot as plt # 创建画布和轴对象 fig, ax = plt.subplots(figsize=(8, 5)) # 在同一个轴上绘制所有谱线 A[0].plot(ax=ax, label='spot 1', color='red', linestyle='-') A[1].plot(ax=ax, label='spot 2', color='green', linestyle='-') A[2].plot(ax=ax, label='spot 3', color='blue', linestyle='-') # 设置坐标轴范围和标签 ax.set_xlim(200.0, 1800.0) ax.set_ylim(0.0, 300.0) ax.set_title("Laser Test", color='darkred') ax.set_xlabel("Raman Shift") ax.set_ylabel("Intensity") # 添加图例 ax.legend(loc='best') # 显示图形 plt.show()
这样所有谱线会合并到同一个图中,完全等效于Matlab的hold on。
内容的提问来源于stack exchange,提问作者Sophie
相关产品推荐
相关产品推荐

