web-dev-qa-db-ja.com

Python 3でmatplotlib 2.0 `ax`オブジェクトに黒い境界線を追加する方法は?

最近、matplotlibでスタイルシートを使用しています。私は本当に_seaborn-white_がきれいに見えるのが好きで、ggplotや_seaborn-whitegrid_のような他のスタイルに境界線を追加できるようにしたいと思っています。

fig, ax = plt.subplots()からaxオブジェクトの周りに黒い境界線を追加するにはどうすればよいですか?

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

enter image description here

以下の答えに応じて:

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

enter image description here

6
O.rka

シーボーンホワイトグリッドとシーボーンホワイトスタイルの違いは、

seaborn-whitegrid

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

seaborn-white

axes.grid: False
axes.edgecolor: .15
axes.linewidth: 1.25

したがって、以下は同じプロットを提供します。

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

enter image description here

おそらくax.spines.set_color()

これらにより、カスタムソリューションの幅広いオプションが提供されます。

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

詳細については、次を参照してください: http://matplotlib.org/api/spines_api.html

3
litepresence

this を見てください。あなたが探しているのは、次の2行です。

ax.patch.set_edgecolor('black')  

ax.patch.set_linewidth('1')  
2
Gabriel Pena