当前位置:   article > 正文

【Datawhale】基于Python的科研论文配图绘制——Task 1_python 科研饼状图 配色

python 科研饼状图 配色

1. 科研论文绘制的应用及意义

科研论文配图作为数据可视化在科研领域的重要应用场景,是研究结果直观、有效的呈现方式,在 学术论文、研究报告、专利申请、科研基金申请 等方面起着举足轻重的作用。不同学术期刊在图名、字体、坐标轴,以及颜色选择、配图格式等方面都有其特有的要求。论文只有符合投稿期刊的配图要求,才能进行下一步的查阅和审核。

2. 科研论文绘制的规范

如下图所示,为Xmind软件绘制的绘图基础简介知识点。
在这里插入图片描述

3. 科研论文绘制的原则

一个简单的表格是这么创建的:

原则内容
必要性原则具体问题具体分析,如果配图可以起到补充说明文字、直观展示结果、引出下文内容等作用,那么它就是必要的
易读性原则完整、准确的标题、标签和图例等可以有效地增强科研论文配图的易读性
一致性原则①配图所表达出的内容与上下文或者指定内容描述一致;②配图数据与上下文保持一致;③插图比例尺和缩放比例大小保持一致;④类似配图各图层要素保持一致

4. 色彩模式

绘图时可选择多种色彩模式,常见的色彩模式包括 RGB 色彩模式、CMYK 色彩模式和 HEX 色彩模式。
在这里插入图片描述

5. 色轮配色

色轮(color wheel)又称色环,一般由 12 种基本颜色按照圆环方式排列组成。常见的色轮配色方案有单色配色方案、互补色配色方案、等距三角配色方案和四角配色方案等。
在这里插入图片描述

①单色配色方案的可视化条形图代码示例(使用ax.bar( )函数)

import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt

from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15

menMeans = (5, 15, 30, 40)
menStd = (2, 3, 4, 5)
ind = np.arange(4)
width = 0.7
labels = ('A', 'B', 'C', 'D')
colors = ["#1826B0","#4C59D8","#717BD8","#081272"] # 颜色配置
fig, ax = plt.subplots(figsize=(4, 3),dpi=100,facecolor="w")
for name,value,err,label,color in zip(labels,menMeans,menStd,labels,colors):
    ax.bar(name,value,yerr=err,label=label,color=color,ec="k",lw=.8,
          capsize=5,error_kw={'linewidth':1})
for spine in ["top","right"]:
    ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(0,50)
ax.legend(frameon=False,handlelength=1.5,handleheight=1.5)
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

代码绘图结果:(可通过调整colors = [“#1826B0”,“#4C59D8”,“#717BD8”,“#081272”]来改变颜色)
![在这里插入图片描述](https://img-blog.csdnimg.cn/7664c1746bf44ad882ae9d42a4795901.png

互补色配色方案的可视化饼状图代码示例(使用ax.pie( )函数)

sizes = [20, 40, 25, 15]
labels = ['A','B','C','D',]
colors=["#1826B0","#4C59D8","#717BD8","#081272"]

fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
explode = (0, 0., 0,0)
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
       shadow=False, startangle=90,colors=colors,
       wedgeprops={'linewidth':.8, 'edgecolor': 'k'},
       textprops={'size': 12,"color":"w"})
ax.legend(labels,loc ='upper left',fontsize=9,handlelength=1.2,handleheight=1.2)
plt.tight_layout()
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

②互补色配色方案的可视化条形图代码示例(使用ax.bar( )函数)

import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt

from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15

menMeans  = (5, 15)
menStd   = (2, 5)
ind  = np.arange(2)
labels = ('A', 'B', )
colors=["#1826B0","#FFBA00"]
fig,ax = plt.subplots(figsize=(4,3),dpi=100,facecolor="w")
for name,value,err,label,color in zip(labels,menMeans,menStd,labels,colors):
    ax.bar(name,value,yerr=err,label=label,color=color,ec="k",lw=.8,
          capsize=5,error_kw={'linewidth':1})
for spine in ["top","right"]:
    ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(0,25)
ax.legend(loc="upper left",frameon=False,
          handlelength=1.5,handleheight=1.5)
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

在这里插入图片描述

互补色配色方案的可视化饼状图代码示例(使用ax.pie( )函数)

sizes = [30, 70]
labels = ['A','B']
colors=["#1826B0","#FFBA00"]

fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
explode = (0, 0.,)
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
       shadow=False, startangle=90,colors=colors,
       wedgeprops={'linewidth':.8, 'edgecolor': 'k'},
       textprops={'size': 12,"color":"w"})
ax.legend(labels,loc ='upper left',fontsize=9,handlelength=1.2,handleheight=1.2)
plt.tight_layout()
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

③等距三角配色方案的可视化图代码示例

import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt

from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15

# 利用等距三角配色方案绘制的可视化配图示例

menMeans  = (5, 15, 35)
menStd   = (2, 3, 5)
ind  = np.arange(3)
labels = ('A', 'B', 'C')
colors=["#1D1AB2","#C9F600","#FF4C00"]
fig,ax = plt.subplots(figsize=(4,3),dpi=100,facecolor="w")
for name,value,err,label,color in zip(labels,menMeans,menStd,labels,colors):
    ax.bar(name,value,yerr=err,label=label,color=color,ec="k",lw=.8,
          capsize=5,error_kw={'linewidth':1})
for spine in ["top","right"]:
    ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(0,50)
ax.legend(frameon=False,handlelength=1.5,handleheight=1.5)
plt.show()

# 利用等距三角配色方案绘制的可视化配图示例
sizes = [20, 50, 30]
labels = ['A','B','C']
colors=["#1D1AB2","#C9F600","#FF4C00"]

fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
explode = (0, 0., 0)
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
       shadow=False, startangle=90,colors=colors,
       wedgeprops={'linewidth':.8, 'edgecolor': 'k'},
       textprops={'size': 12,"color":"w"})
ax.legend(labels,loc ='upper left',fontsize=9,handlelength=1.2,handleheight=1.2)
plt.tight_layout()

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

在这里插入图片描述

在这里插入图片描述

④四角配色方案的可视化图代码示例

import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt

from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 15
rc['tick.labelsize'] = 13
rc["suptitle.size"] = 15

# 利用等距三角配色方案绘制的可视化配图示例

menMeans  = (5, 15, 35)
menStd   = (2, 3, 5)
ind  = np.arange(3)
labels = ('A', 'B', 'C')
colors=["#1D1AB2","#C9F600","#FF4C00"]
fig,ax = plt.subplots(figsize=(4,3),dpi=100,facecolor="w")
for name,value,err,label,color in zip(labels,menMeans,menStd,labels,colors):
    ax.bar(name,value,yerr=err,label=label,color=color,ec="k",lw=.8,
          capsize=5,error_kw={'linewidth':1})
for spine in ["top","right"]:
    ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(0,50)
ax.legend(frameon=False,handlelength=1.5,handleheight=1.5)
plt.show()

# 利用等距三角配色方案绘制的可视化配图示例
sizes = [20, 50, 30]
labels = ['A','B','C']
colors=["#1D1AB2","#C9F600","#FF4C00"]

fig,ax = plt.subplots(figsize=(4,3.5),dpi=100,facecolor="w")
explode = (0, 0., 0)
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
       shadow=False, startangle=90,colors=colors,
       wedgeprops={'linewidth':.8, 'edgecolor': 'k'},
       textprops={'size': 12,"color":"w"})
ax.legend(labels,loc ='upper left',fontsize=9,handlelength=1.2,handleheight=1.2)
plt.tight_layout()

plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

在这里插入图片描述
在这里插入图片描述
注意课程代码中需要安装import scienceplots库!!!

6. 颜色主题

**下面我们对比Python中三种库的颜色主题(Matplotlib,Seaborn 和 SciencePlots) **

import numpy as np
import pandas as pd
import seaborn as sns
import proplot as pplt
import matplotlib.pyplot as plt
import scienceplots
from proplot import rc
rc["font.family"] = "Times New Roman"
rc["axes.labelsize"] = 13
rc['tick.labelsize'] = 11
rc["suptitle.size"] = 15

tips = sns.load_dataset("tips")

# a)Matplotlib的默认颜色主题
matplotlib_colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']

fig,ax = plt.subplots(figsize=(3,3.2),dpi=100,facecolor="w")
ax = sns.violinplot(x="day", y="total_bill", data=tips,
                    palette=matplotlib_colors,
                    saturation=1)
for spine in ["top","right"]:
    ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(-10,60)
ax.set_xlabel("Class")
ax.set_ylabel("Value")
plt.tight_layout()
plt.show()

# b)Seaborn的默认颜色主题
fig,ax = plt.subplots(figsize=(3,3.2),dpi=100,facecolor="w")
ax = sns.violinplot(x="day", y="total_bill", data=tips,saturation=1)
for spine in ["top","right"]:
    ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_ylim(-10,60)
ax.set_xlabel("Class")
ax.set_ylabel("Value")
plt.tight_layout()
plt.show()

# c)SciencePlots的默认颜色主题(需安装Scienceplots包(pip install SciencePlots),不同版本引用方式有所不同,请注意!)
with plt.style.context(['science']):
    from proplot import rc
    rc["xtick.minor.visible"] = False
    rc["ytick.minor.visible"] = False
    rc["xtick.major.pad"] =5
    fig,ax = plt.subplots(figsize=(3,3.2),dpi=100,facecolor="w")
    ax = sns.violinplot(x="day", y="total_bill", data=tips,saturation=1)
    for spine in ["top","right"]:
        ax.spines[spine].set_visible(False)
    ax.tick_params(top=False,right=False)
    ax.grid(False)
    ax.set_ylim(-10,60)
    ax.set_xlabel("Class")
    ax.set_ylabel("Value")
    plt.tight_layout()
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Matplotlib 库的颜色主题主要包括 3 种类型:单色系(sequential)、双色渐变色系(diverging)
和多色系(qualitative)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Seaborn 库同样也包含了以上色系,使用中Tips 数据集绘制的单色系、双色渐变色系和多色系可视化配图,如下图所示(具体为单色系中的 ylgnbu 色系、双色渐变色系中的 seismic 色系和多色系中的set1 色系)。
在这里插入图片描述

7. 配色工具

常用的配色工具有 Color Scheme Designer 网站中的高级在线配色器、Adobe 旗下的在线配色工具 Adobe Color 和颜色主题搭配网站 ColorBrewer 2.0
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号