如何在Python 3中为matplotlib 2.0`ax`对象添加黑色边框? [英] How to add black border to matplotlib 2.0 `ax` object In Python 3?

查看:402
本文介绍了如何在Python 3中为matplotlib 2.0`ax`对象添加黑色边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在matplotlib中使用样式表.我真的很喜欢seaborn-white的外观多么干净,我希望能够将边框添加到其他样式,例如ggplotseaborn-whitegrid.

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.

如何在fig, ax = plt.subplots()中的ax对象周围添加黑色边框?

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")

针对以下答案:

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')

推荐答案

seaborn-whitegrid和seaborn-white样式之间的区别是

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

seaborn-whitegrid

axes.grid: True
axes.edgecolor: .8
axes.linewidth: 1

深蓝色

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")

这篇关于如何在Python 3中为matplotlib 2.0`ax`对象添加黑色边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆