python - 没有框架的 Matplotlib 子图面颜色
问题描述
当我使用 matplotlib 时,我想控制(子)图的表面颜色。
例如,如果我跑步,
tips = sns.load_dataset("tips")
f, ax = plt.subplots(subplot_kw=dict(facecolor=".9"))
sns.scatterplot(data=tips, x="total_bill", y="tip")
我明白了,
默认的白色面部颜色更改为灰色。虽然跑步,
tips = sns.load_dataset("tips")
rows, cols = 1, 1
f, axs = plt.subplots( rows, cols, subplot_kw=dict(facecolor=".9"))
plt.subplot( rows, cols, 1)
sns.scatterplot(data=tips, x="total_bill", y="tip")
给,
在每个子图命令中明确设置面颜色可以解决它,即
tips = sns.load_dataset("tips")
rows, cols = 1, 1
f, axs = plt.subplots( rows, cols)
plt.subplot( rows, cols, 1, facecolor=".9")
sns.scatterplot(data=tips, x="total_bill", y="tip")
但是,当我使用plt.box( False)
或
ax = plt.subplot( rows, cols, 1, facecolor = ".9")
ax.set_frame_on( False)
或者
plt.subplot( rows, cols, 1, facecolor = ".9", frame_on = False)
问题又回来了。看来,没有框架的自定义面部颜色不能作为“配置”来满足。
从frame_on
属性的文档中,“设置是否绘制轴矩形补丁。”,https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_frame_on.html#matplotlib.axes.Axes.set_frame_on。因此,它看起来像matplotlib
添加了一个补丁来改变面部颜色。
那么是否可以更改此补丁的边缘颜色?我不想有这个“厚”的框架边框线。添加我自己的补丁是唯一的选择吗?
编辑
当我尝试这个时,
tips = sns.load_dataset("tips")
rows, cols = 2, 1
f, axs = plt.subplots( rows, cols, figsize = ( 10, 10), subplot_kw = dict( facecolor = ".9"))
plt.subplot( rows, cols, 1)
sns.scatterplot(data=tips, x="total_bill", y="tip", ax = axs[ 0])
axs[ 0].grid( color = 'w', linewidth = 1)
[ axs[ 0].spines[ s].set_visible( False) for s in axs[ 0].spines.keys()]
我明白了,
问题是我正在调用plt.subplot()
创建一个新轴来覆盖位置的轴( rows, cols, 1)
。删除此行即可解决问题。
谢谢。
解决方案
推荐阅读
- c# - 自定义 C# 编译器警告
- javascript - 将 Firebase 托管连接到域后,配置中的 authDomain 或 databaseURL 是否更改?
- wordpress - BuddyPress 个人资料和封面图片无法上传或裁剪
- python - 对 Sympy 向量进行羔羊化
- c++ - 为什么不将临时对象传递给对象初始化调用复制构造函数?
- android - 带有导航库的 Android 应用静态快捷方式
- jquery - 将 Flickity 重置回幻灯片 1
- python - 烧瓶登录不会跨请求持续存在
- sql - SQL 中字符串末尾的 % 符号有什么作用?
- mysql - JSON包含空数组的MySQL