How to add black border to matplotlib 2.0 `ax` object In Python 3?

北城以北 提交于 2020-01-02 04:34:21

问题


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

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


回答1:


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




回答2:


You probably want ax.spines.set_color()

These will give you a broad range of options for custom solutions:

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(0.5)
fig.patch.set_facecolor('0.15')

For more details see: http://matplotlib.org/api/spines_api.html




回答3:


Take a look at this. What you're looking for are these two lines:

ax.patch.set_edgecolor('black')  

ax.patch.set_linewidth('1')  


来源:https://stackoverflow.com/questions/43265731/how-to-add-black-border-to-matplotlib-2-0-ax-object-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!