Python seaborn超级细节完结篇-联合美化
本文分享Python seaborn中联合比例尺度context、风格style及配色palette美化图形。
续前三篇:
Python seaborn超级细节篇-比例尺度context
seaborn提供了一个set_theme函数,可同时联合章节5.1中的context、章节5.2中的style、章节5.3中palette美化图表。
set_theme使用方法,
import seaborn as snssns.set_theme(
context='notebook', #效果同章节5.1.2 set_context设置context内容
style='darkgrid', #效果同章节5.2.2 set_style()设置style内容
palette='deep', #效果同章节5.3 palette设置图形配色
font='sans-serif', #设置
font_scale=1, #设置文字缩放
color_codes=
True, #如果为True,并且调色板是一个seaborn的调色板,
#则重新映射简写的颜色代码(例如:“b”、“g”、“r”等)为该调色板中的颜色
rc=None) # 自定义matplotlib rcParams中任意参数
以上是set_theme的默认参数,类似章节5.1和5.2,如果要调用默认设置,sns.set_theme()即可。
以下实例介绍set_theme参数修改对图形外观的影响,
5.4.1 不使用set_theme
matplotlib默认参数绘制boxplot,
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np# 模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
## 绘图
plt.boxplot(df.values)
plt.title('Matplotlib Default Parameters')
plt.show()
当seaborn不使用set_theme时,直接使用seaborn中拥有的matplotlib默认参数绘制图,
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np# 模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
## 绘图
sns.boxplot(data=df).set_title('Seaborn Default Parameters')
plt.show()
使用set_theme默认参数绘图,
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np# 模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
#
sns.set_theme() #使用set_theme默认配置
## 绘图
sns.boxplot(data=df).set_title('Theme: set_theme()')
plt.show()
效果还是很明显的。
5.4.2 set_theme设置context
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np# 模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
#
sns.set_theme(context='notebook') #使用set_theme设置context
## 绘图
sns.boxplot(data=df).set_title('Theme: context')
plt.show()
!
多参考章节5.1中的context。
5.4.3 set_theme设置style
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np# 模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
#
sns.set_theme(style='whitegrid') #使用set_theme设置style
## 绘图
sns.boxplot(data=df).set_title('Theme: style')
plt.show()
更多请章节5.2中的style。
5.4.4 set_theme设置palette
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np# 模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
#
sns.set_theme(palette='Set1') #使用set_theme设置palette
## 绘图
sns.boxplot(data=df).set_title('Theme: palette')
plt.show()
更多请参考章节5.3中的palette。
5.4.5 set_theme设置rc
set_theme支持修改set_theme()中的默认参数。
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as npmpl.rc_file_defaults()
# 设置模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
# 修改rc参数
custom_set = {
"font.family": "Songti SC",
"axes.spines.left": True, #y坐标轴开启
"axes.spines.bottom": True, #x坐标轴开启
"axes.spines.top": False, #上方坐标轴关闭
"axes.spines.right": False, #右侧坐标轴关闭
"axes.edgecolor": "gray", #坐标轴颜色
"axes.linewidth": 0.4, #坐标轴宽度
"grid.linewidth": 0.3, #网格线宽度
"grid.alpha": 1, #网格线透明度
"legend.frameon": True, #图例边框开启
"legend.framealpha": 0.5 ##图例边框透明度
}
sns.set_theme(rc=custom_set)
# 绘制箱线图
sns.boxplot(data=df)
plt.show()
上图是在下图默认参数修改rc而来,
5.4.6 set_theme同时设置context、style、palette、rc
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as npmpl.rc_file_defaults()
# 设置模拟数据
np.random.seed(42)
data = {f'Group {i}': np.random.normal(i, 1, 100) for i in range(1, 5)}
df = pd.DataFrame(data)
# 同时设置
sns.set_theme(context='notebook', style='whitegrid', palette='Set1', rc=custom_set)
# 绘制箱线图
sns.boxplot(data=df)
plt.show()
-END-
每类图表包含详细代码+详细代码注释,多达8000行代码,例如,
如何加入学习?
👇加入学习(收费、备注:169)
✅解锁获取6大权限:
8000+行代码(复制即可运行) 在线教程文档(清晰目录) 依赖数据(免费下载) 交流群 后续更新 定期抽最新机器学习、数据科学类书籍