【SCI绘图】【曲线图系列2 python】多类别标签对比的曲线图

这篇博客分享了如何使用Python为SCI论文绘制多类别标签对比的曲线图,包括环境准备、数据读取和绘图展示,适用于科研绘图需求。

SCI,CCF,EI及核心期刊绘图宝典,爆款持续更新,助力科研!

本期分享:

【SCI绘图】【曲线图系列2 python】多类别标签对比的曲线图,文末附完整代码

1.环境准备

python 3

import proplot as pplt
import pandas as pd

from proplot import rc

2.数据示例与读取

数据示例:

     68.52769   1.01941334E-118   1.04975387E-287   3.07499527E-220   4.62365918E-169   2.32835613E-134
     68.58945   3.94732386E-118   8.59329295E-287   1.94218811E-219   2.33402749E-168   9.88293492E-134
     68.65121   1.51971781E-117   6.98480839E-286   1.21860660E-218   1.17091842E-167   4.17021496E-133
     68.71298   5.81756808E-117   5.63747055E-285   7.59578697E-218   5.83793125E-167   1.74935402E-132
     68.77474   2.21436321E-116   4.51815464E-284   4.70360236E-217   2.89277108E-166   7.29548983E-132
     68.83650   8.38099508E-116   3.59581184E-283   2.89366667E-216   1.42463282E-165   3.02481879E-131
     68.89826   3.15422077E-115   2.84186785E-282   1.76863385E-215   6.97327187E-165   1.24687538E-130
     68.96003   1.18045311E-114   2.23045665E-281   1.07401485E-214   3.39254761E-164   5.11017864E-130
     69.02179   4.39314840E-114   1.73851547E-280   6.48002835E-214   1.64052134E-163   2.08232725E-129
     69.08355   1.62586232E-113   1.34576744E-279   3.88462479E-213   7.88524556E-163   8.43669246E-129
     69.14532   5.98387683E-113   1.03461776E-278   2.31386509E-212   3.76735665E-162   3.39872639E-128
     69.20708   2.19019452E-112   7.89987107E-278   1.36947767E-211   1.78919283E-161   1.36141743E-127

读取数据:

# 读取 txt 文件中的数据
data = pd.read_csv('uv_curve.txt', header=None, delim_whitespace=True)

# 颜色、曲线格式以及标签
color_list = ['black', 'red', 'orange', 'blue', 'green']
label_list = ['label1', 'label2', 'label3', 'label4', 'label5']
style_list = ['-', '--', '--', '--', '--']

3.绘图展示

画布设置:

# 设置绘图的默认参数,如字体、字号等
rc['font.name'] = 'Arial'
rc['title.size'] = 14
rc['label.size'] = 12
rc['font.size'] = 10.5
rc['tick.width'] = 1.3
rc['meta.width'] = 1.3
rc['label.weight'] = 'bold'
rc['tick.labelweight'] = 'bold'
rc['ytick.major.size'] = 4.6
rc['ytick.minor.size'] = 2.5
rc['xtick.major.size'] = 4.6
rc['xtick.minor.size'] = 2.5

绘制逻辑:

# 创建画布
fig, ax = pplt.subplots(figsize=(6 * 0.9, 4 * 0.9))

# 绘制多曲线图
x = data.iloc[:, 0]
for i in range(len(data.columns) - 1):
    y = data.iloc[:, i + 1]
    ax.plot(x, y, linewidth=1.3, color=color_list[i], 
            label=label_list[i], linestyle=style_list[i])

# 设置图例
ax.legend(loc='ur', ncols=1, fontweight='bold', frame=False)

# 格式化图像
fig.format(
    grid=False, ylabel='Absorbance', xlabel='Wavelength (nm)',
    xlim=(80, 200), xminorlocator=10, xlocator=20, 
    ylim=(0, 10000), yminorlocator=1000, ylocator=2000
)

fig.show()

fig.savefig('uv.png', bbox_inches='tight', dpi=300)

完整代码:

# -*- coding: utf-8 -*-
import proplot as pplt
import pandas as pd

from proplot import rc

# 读取 txt 文件中的数据
data = pd.read_csv('uv_curve.txt', header=None, delim_whitespace=True)

# 颜色、曲线格式以及标签
color_list = ['black', 'red', 'orange', 'blue', 'green']
label_list = ['label1', 'label2', 'label3', 'label4', 'label5']
style_list = ['-', '--', '--', '--', '--']

# 设置绘图的默认参数,如字体、字号等
rc['font.name'] = 'Arial'
rc['title.size'] = 14
rc['label.size'] = 12
rc['font.size'] = 10.5
rc['tick.width'] = 1.3
rc['meta.width'] = 1.3
rc['label.weight'] = 'bold'
rc['tick.labelweight'] = 'bold'
rc['ytick.major.size'] = 4.6
rc['ytick.minor.size'] = 2.5
rc['xtick.major.size'] = 4.6
rc['xtick.minor.size'] = 2.5

# 创建画布
fig, ax = pplt.subplots(figsize=(6 * 0.9, 4 * 0.9))

# 绘制多曲线图
x = data.iloc[:, 0]
for i in range(len(data.columns) - 1):
    y = data.iloc[:, i + 1]
    ax.plot(x, y, linewidth=1.3, color=color_list[i], 
            label=label_list[i], linestyle=style_list[i])

# 设置图例
ax.legend(loc='ur', ncols=1, fontweight='bold', frame=False)

# 格式化图像
fig.format(
    grid=False, ylabel='Absorbance', xlabel='Wavelength (nm)',
    xlim=(80, 200), xminorlocator=10, xlocator=20, 
    ylim=(0, 10000), yminorlocator=1000, ylocator=2000
)

fig.show()

fig.savefig('uv.png', bbox_inches='tight', dpi=300)

<think>好的,我现在需要回答用户关于如何使用Python的Matplotlib、Seaborn和Plotly进行科研绘图的问题。首先,我得回忆这三个库的基本特点和适用场景,然后结合用户提供的引用内容,整理出教程结构。 用户提供的引用里提到了Matplotlib的线图示例和Seaborn的散点图示例,但Plotly的例子似乎没有直接给出。我需要确保每个库都有基础使用说明,并涵盖科研绘图的常见需求,比如数据可视化、图表美化、导出等。 接下来,我要分步骤讲解每个库的使用方法,可能包括安装、基本绘图流程、常用图表类型以及自定义样式。例如,Matplotlib作为基础库,适合讲解线图、柱状图等;Seaborn则在统计图表上更便捷,比如热力图、箱线图;Plotly的优势在于交互式图表,适合动态展示。 需要注意用户提到的科研绘图可能对图表的清晰度、格式(如导出为PDF或SVG)有较高要求,所以需要包含调整分辨率和导出设置的指导。同时,要确保代码示例正确,并参考用户提供的引用内容,比如背景颜色设置等。 另外,用户的问题还要求生成相关问题,我需要根据回答内容提出几个相关的问题,例如性能优化、高级功能或比较不同库的优缺点。最后,检查是否符合格式要求,如数学表达式的正确使用、引用标识的添加等。</think>以下是Python科研绘图库Matplotlib、Seaborn和Plotly的使用教程,分步骤说明: ### 一、Matplotlib基础 1. **安装与导入** ```bash pip install matplotlib numpy ``` ```python import matplotlib.pyplot as plt import numpy as np ``` 2. **线图绘制** ```python x = np.arange(0, 10, 0.1) y = np.sin(x) plt.plot(x, y, color='#1f77b4', linestyle='--', linewidth=2) plt.xlabel('x轴', fontsize=12) plt.ylabel('y轴', fontsize=12) plt.title('正弦函数曲线', fontsize=14) plt.grid(alpha=0.5) plt.savefig('sin_curve.pdf', dpi=300, bbox_inches='tight') # 保存矢量图 plt.show() ``` 该代码生成带网格的虚线正弦曲线,保存为科研论文常用PDF格式[^1]。 3. **多子图布局** ```python fig, axs = plt.subplots(2, 1, figsize=(8,6)) axs[0].plot(x, np.sin(x)) axs[1].scatter(x, np.cos(x), marker='^') plt.tight_layout() ``` ### 二、Seaborn进阶 1. **统计图表绘制** ```python import seaborn as sns tips = sns.load_dataset("tips") # 箱线图+蜂群图 plt.figure(figsize=(8,5)) sns.boxplot(x="day", y="total_bill", data=tips, width=0.5) sns.swarmplot(x="day", y="total_bill", data=tips, color=".2") plt.xticks(fontsize=10) ``` 2. **热力图绘制** ```python flights = sns.load_dataset("flights").pivot("month", "year", "passengers") sns.heatmap(flights, annot=True, fmt="d", cmap="YlGnBu") ``` 3. **主题设置** ```python sns.set_style("whitegrid", {'grid.linestyle': ':'}) # 点线网格 sns.set_context("paper", font_scale=1.2) # 适配论文尺寸 ``` ### 三、Plotly交互式 1. **3D曲面图** ```python import plotly.graph_objects as go x = np.outer(np.linspace(-5,5,50), np.ones(50)) y = x.T z = np.sin(np.sqrt(x**2 + y**2)) fig = go.Figure(data=[go.Surface(z=z)]) fig.update_layout( title='3D曲面图', scene=dict( xaxis_title='X轴', yaxis_title='Y轴', zaxis_title='Z轴' ), width=800, height=600 ) fig.show() ``` 2. **导出为HTML** ```python fig.write_html("3d_plot.html") # 保留交互功能 ``` ### 四、科研绘图规范 1. **字体设置** ```python plt.rcParams['font.family'] = 'Times New Roman' # 学术论文标准字体 plt.rcParams['mathtext.fontset'] = 'stix' # 数学符号字体 ``` 2. **颜色规范** ```python sci_colors = ["#2E86C1", "#CB4335", "#27AE60", "#884EA0"] # 避免使用彩虹色系 ``` 3. **分辨率设置** ```python plt.savefig('figure.tiff', dpi=600, format='tiff', pil_kwargs={"compression": "tiff_lzw"}) # 满足期刊要求 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪电科创

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值