python环境是黑框吗,如何在Python 3中为matplotlib 2.0`ax`对象添加黑色边框?

I've been using style sheets in matplotlib lately. I really like how clean the seaborn-white looks and I want to be able to add the border to other styles like ggplot or seaborn-whitegrid.

How can I add a black border around my ax object from fig, ax = plt.subplots()?

import pandas as pd

import numpy as np

from collections import *

Se_data = pd.Series(Counter(np.random.randint(0,10,100)))

with plt.style.context("seaborn-whitegrid"):

fig, ax = plt.subplots()

Se_data.plot(kind="barh", ax=ax, title="No Border")

with plt.style.context("seaborn-white"):

fig, ax = plt.subplots()

Se_data.plot(kind="barh", ax=ax, title="With Border")

om8rA.png

In response to the answer below:

Se_data = pd.Series(Counter(np.random.randint(0,10,100)))

with plt.style.context("seaborn-whitegrid"):

fig, ax = plt.subplots()

Se_data.plot(kind="barh", ax=ax, title="No Border")

ax.spines['bottom'].set_color('0.5')

ax.spines['top'].set_color(None)

ax.spines['right'].set_color('0.5')

ax.spines['left'].set_color(None)

ax.patch.set_facecolor('0.1')

plt.grid(b=True, which='major', color='0.2', linestyle='-')

plt.grid(b=True, which='minor', color='0.2', linestyle='-')

ax.tick_params(axis='x', colors='0.7', which='both')

ax.tick_params(axis='y', colors='0.7', which='both')

ax.yaxis.label.set_color('0.9')

ax.xaxis.label.set_color('0.9')

ax.margins(5)

fig.patch.set_facecolor('0.15')

z0Exh.png

解决方案

The difference between the seaborn-whitegrid and the seaborn-white styles are

seaborn-whitegrid

axes.grid: True

axes.edgecolor: .8

axes.linewidth: 1

seaborn-white

axes.grid: False

axes.edgecolor: .15

axes.linewidth: 1.25

The following will thus provide identical plots:

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np

from collections import *

Se_data = pd.Series(Counter(np.random.randint(0,10,100)))

with plt.style.context("seaborn-whitegrid"):

plt.rcParams["axes.edgecolor"] = "0.15"

plt.rcParams["axes.linewidth"] = 1.25

fig, ax = plt.subplots()

Se_data.plot(kind="barh", ax=ax, title="No Border")

with plt.style.context("seaborn-white"):

plt.rcParams["axes.grid"] = True

fig, ax = plt.subplots()

Se_data.plot(kind="barh", ax=ax, title="With Border")

VWwLz.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值