python - 在 matplotlib 中更改阴影颜色
问题描述
谢谢你帮我把这张图弄对了!
我现在有另一个问题,我希望将阴影线的颜色更改为灰色。
我正在使用 matplotlib 版本 1.5.3'。我试过 mlp.rcParams['hatch.color'] = 'k'
但这似乎不起作用...
这是我已经拥有的图形的代码,感谢您:
import seaborn as sns
import matplotlib.pyplot as plt
mypallet = sns.color_palette([(190/256,7/256, 18/256),(127/256, 127/256, 127/256)])
import itertools
import numpy as np
plt.rcParams['figure.figsize'] = 7, 5
tips = sns.load_dataset("tips")
tips[(tips.day=='Thur') & (tips.sex=='Female') ] = np.nan
print(sns.__version__)
print(tips.head())
# Bigger than normal fonts
sns.set(font_scale=1.5)
ax = sns.swarmplot(x="day", y="total_bill", hue="sex",
data=tips, dodge=True, color='k')
#get first patchcollection
c0 = ax.get_children()[0]
x,y = np.array(c0.get_offsets()).T
#Add .2 to x values
xnew=x+.2
offsets = list(zip(xnew,y))
#set newoffsets
c0.set_offsets(offsets)
ax = sns.barplot(x="day", y="total_bill", hue="sex",
data=tips, capsize=0.1, alpha=0.8,
errwidth=1.25, ci=None, palette=mypallet)
xcentres = [0.2, 1, 2, 3]
delt = 0.2
xneg = [x-delt for x in xcentres]
xpos = [x+delt for x in xcentres]
xvals = xneg + xpos
xvals.sort()
yvals = tips.groupby(["day", "sex"]).mean().total_bill
yerr = tips.groupby(["day", "sex"]).std().total_bill
(_, caps, _)=ax.errorbar(x=xvals, y=yvals, yerr=yerr, capsize=4,
ecolor="red", elinewidth=1.25, fmt='none')
for cap in caps:
cap.set_markeredgewidth(2)
handles, labels = ax.get_legend_handles_labels()
l = ax.legend(handles[0:2], labels[0:2]) # changed based on https://stackoverflow.com/a/42768387/8508004
#sns.ax.ylim([0,60]) #original
ax.set_ylim([0,60]) # adapted from https://stackoverflow.com/a/49049501/8508004 and change to legend
ax.set_ylabel("Out-of-sample R2") # based on https://stackoverflow.com/a/46235777/8508004
ax.set_xlabel("") # based on https://stackoverflow.com/a/46235777/8508004
for i, bar in enumerate(ax.patches):
hatch = '///'
bar.set_hatch(hatch)
bar.set_x(bar.get_x() + bar.get_width()/2)
break
我想将填充图案的颜色从黑色更改为灰色:(127/256、127/256、127/256)
解决方案
添加plt.rcParams['hatch.linewidth'] = 3
和使用set_edgecolor
,认为 `plt.rcParams['hatch.color'] = 'k' 不起作用的事实是一个错误。
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
mypallet = sns.color_palette([(190/256,7/256, 18/256),(127/256, 127/256, 127/256)])
import itertools
import numpy as np
plt.rcParams['figure.figsize'] = 7, 5
plt.rcParams['hatch.linewidth'] = 3
tips = sns.load_dataset("tips")
tips[(tips.day=='Thur') & (tips.sex=='Female') ] = np.nan
print(sns.__version__)
print(tips.head())
# Bigger than normal fonts
sns.set(font_scale=1.5)
ax = sns.swarmplot(x="day", y="total_bill", hue="sex",
data=tips, dodge=True, color='k')
#get first patchcollection
c0 = ax.get_children()[0]
x,y = np.array(c0.get_offsets()).T
#Add .2 to x values
xnew=x+.2
offsets = list(zip(xnew,y))
#set newoffsets
c0.set_offsets(offsets)
ax = sns.barplot(x="day", y="total_bill", hue="sex",
data=tips, capsize=0.1, alpha=0.8,
errwidth=1.25, ci=None, palette=mypallet)
xcentres = [0.2, 1, 2, 3]
delt = 0.2
xneg = [x-delt for x in xcentres]
xpos = [x+delt for x in xcentres]
xvals = xneg + xpos
xvals.sort()
yvals = tips.groupby(["day", "sex"]).mean().total_bill
yerr = tips.groupby(["day", "sex"]).std().total_bill
(_, caps, _)=ax.errorbar(x=xvals, y=yvals, yerr=yerr, capsize=4,
ecolor="red", elinewidth=1.25, fmt='none')
for cap in caps:
cap.set_markeredgewidth(2)
handles, labels = ax.get_legend_handles_labels()
l = ax.legend(handles[0:2], labels[0:2]) # changed based on https://stackoverflow.com/a/42768387/8508004
#sns.ax.ylim([0,60]) #original
ax.set_ylim([0,60]) # adapted from https://stackoverflow.com/a/49049501/8508004 and change to legend
ax.set_ylabel("Out-of-sample R2") # based on https://stackoverflow.com/a/46235777/8508004
ax.set_xlabel("") # based on https://stackoverflow.com/a/46235777/8508004
for i, bar in enumerate(ax.patches):
hatch = '///'
bar.set_hatch(hatch)
bar.set_edgecolor('k')
bar.set_x(bar.get_x() + bar.get_width()/2)
break
输出:
推荐阅读
- python - 如果输入包含 NaN,则通过 Pytorch Element-Wise Operation 反向传播
- excel - 在 VBA-excel 中使用数组进行“计数排序”
- java - 调用 deleteRecords Kafka Admin Client Java API 时,消息不会从文件系统中删除
- postman - Why does Postman change saved API requests
- javascript - 图片加载失败
- python - 计算字符串中小写和大写字母数量的Python函数
- javascript - TS 节点:找不到源文件
- visual-studio-code - 在 VS Code 上,为什么它会在配置 JS 文件中的每个冒号处告诉我,我应该使用分号?
- javascript - 如何在 componentDidMount 函数中调用异步函数?
- laravel - 在 Eloquent 中,“with”如何导致多个查询