Python-matplotlib 多类别学术散点图制作

2021-02-22 15:10:48 浏览数 (1)

01. 引言

本期推文回归学术图表的绘制教程,本次的推文也是在查看SCI论文时发现,图表简单明了且使用较多,接下来我们通过构建虚拟数据进行符合出版的多类别散点图绘制。

02. 数据构建及可视化绘制

我们构建6组虚拟数据进行绘制,具体如下:

绘图代码具体如下:

代码语言:javascript复制
#开始绘图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

plt.rcParams['font.family'] = "Times New Roman"
x = np.arange(0,len(scatter),1)
y1 = scatter.D1.values
y2 = scatter.D2.values
y3 = scatter.D3.values
y4 = scatter.D4.values
y5 = scatter.D5.values
y6 = scatter.D6.values

fig,ax = plt.subplots(figsize=(4.5,3),dpi=300)
scatter_01 = ax.plot(x,y1,marker='s',c='k',lw=.5,markerfacecolor='dimgray',markeredgecolor='dimgray',
                     label='D1')
scatter_02 = ax.plot(x,y2,marker='s',c='k',ls='--',lw=.5,markerfacecolor='white',markeredgewidth=.4,
                    markeredgecolor='k',label='D2')
scatter_03 = ax.plot(x,y3,marker='o',c='k',lw=.8,ls=':',markerfacecolor='dimgray',markeredgecolor='dimgray',
                    label='D3')
scatter_04 = ax.plot(x,y4,marker='o',c='k',lw=.5,markerfacecolor='white',markeredgewidth=.4,
                    markeredgecolor='k',label='D4')

scatter_05 = ax.plot(x,y5,marker='^',c='k',lw=.5,ls='-.',markerfacecolor='k',markeredgecolor='k',
                    label='D5')
scatter_06 = ax.plot(x,y6,marker='^',c='k',ls='--',lw=.5,markerfacecolor='white',markeredgewidth=.4,
                    markeredgecolor='k',label='D6')

#修改次刻度
yminorLocator = MultipleLocator(2.5) #将此y轴次刻度标签设置为0.1的倍数
ax.yaxis.set_minor_locator(yminorLocator)
ax.tick_params(which='major',direction='out',length=4,width=.5)
ax.tick_params(which='minor',direction='out',length=2,width=.5)
ax.set_ylim(bottom=10,top=45)
for spine in ['top','bottom','left','right']:
    ax.spines[spine].set_linewidth(.5)
ax.legend(frameon=False,ncol=3,loc='upper center',fontsize=8.5)

text_font = {'size':'13','weight':'bold','color':'black'}
ax.text(.03,.91,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.87,.04,'nVisualization by DataCharm',transform = ax.transAxes,
        ha='center', va='center',fontsize = 4,color='black',fontweight='bold',family='Roboto Mono')
plt.savefig(r'F:DataCharmSCI paper plotssci_scatter.png',width=4.5,height=3,
            dpi=900,bbox_inches='tight')
plt.show()

知识点:

(1)ax.plot()函数marker的具体设置,本期推文,我们分别设置's' 矩形,'o'圆形,'^' 上三角形。其他类型如下:

(2)ax.plot()函数linestyle(ls)连接线的类型,matplotlib提供的类别如下:

下面列举ax.plot()其他主要参数如下:

(3) 副刻度设置:ax.yaxis.set_minor_locato()

(4) 轴脊(spines)宽度设置:

代码语言:javascript复制
for spine in ['top','bottom','left','right']:
    ax.spines[spine].set_linewidth(.5)

其他详细内容可以参考官网

得到的图形如下:

03. 总结

本期推文回归学术图表绘制教程:多类别散点图。涉及连接线、颜色、刻度等属性参数的设置,教程相对简单,希望能够帮到大家。欢迎进群讨论啊

0 人点赞