Python 在matplotlibrc中设置脊椎

Python 在matplotlibrc中设置脊椎,python,matplotlib,Python,Matplotlib,出于一个奇怪的原因,我找不到在Python的matplotlibrc文件中指定spines配置的方法。您知道如何使matplotlib在默认情况下不绘制上部和右侧脊椎吗? (来源:) 有关matplotlib中脊椎的更多信息,请参见 谢谢为了隐藏子批次的右侧和顶部脊椎,您需要将相关脊椎的颜色设置为'none',并将勾号位置设置为'left',将ytick设置为'bottom'(为了隐藏勾号和脊椎) 不幸的是,目前无法通过matplotlibrc访问这些文件。验证matplotlibrc中指定的

出于一个奇怪的原因,我找不到在Python的matplotlibrc文件中指定spines配置的方法。您知道如何使matplotlib在默认情况下不绘制上部和右侧脊椎吗?
(来源:)

有关matplotlib中脊椎的更多信息,请参见


谢谢

为了隐藏子批次的右侧和顶部脊椎,您需要将相关脊椎的颜色设置为
'none'
,并将勾号位置设置为
'left'
,将ytick设置为
'bottom'
(为了隐藏勾号和脊椎)

不幸的是,目前无法通过
matplotlibrc
访问这些文件。验证
matplotlibrc
中指定的参数,然后将其存储在名为
rcParams
的dict中。然后由各个模块检查此dict中的密钥,其值将作为其默认值。如果他们没有检查其中一个选项,则无法通过
rc
文件更改该选项

由于
rc
系统的性质以及spine的编写方式,修改代码以实现这一点并不简单:

脊椎当前通过用于定义轴颜色的相同
rc
参数获得颜色;如果不隐藏所有轴图形,则无法将其设置为“无”。它们也不知道它们是
顶部
右侧
左侧
,还是
底部
——它们实际上只是存储在dict中的四个独立的脊椎。单个脊椎对象不知道它们组成了绘图的哪一侧,因此,在脊椎初始化期间,您不能只添加新的
rc
params并分配适当的参数

self.set_edgecolor( rcParams['axes.edgecolor'] )
(./matplotlib/lib/matplotlib/spines.py,_init__(),第54行)

如果您有大量的现有代码,因此手动将axis参数添加到每个axis参数将过于繁重,您可以交替使用辅助函数迭代所有axis对象并为您设置值

下面是一个例子:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import show

# Set up a default, sample figure. 
fig = plt.figure()
x = np.linspace(-np.pi,np.pi,100)
y = 2*np.sin(x)

ax = fig.add_subplot(1,2,2)
ax.plot(x,y)
ax.set_title('Normal Spines')

def hide_spines():
    """Hides the top and rightmost axis spines from view for all active
    figures and their respective axes."""

    # Retrieve a list of all current figures.
    figures = [x for x in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
    for figure in figures:
        # Get all Axis instances related to the figure.
        for ax in figure.canvas.figure.get_axes():
            # Disable spines.
            ax.spines['right'].set_color('none')
            ax.spines['top'].set_color('none')
            # Disable ticks.
            ax.xaxis.set_ticks_position('bottom')
            ax.yaxis.set_ticks_position('left')

hide_spines()
show()

只需在
show()
之前调用
hide\u spines()
,它就会将它们隐藏在
show()
显示的所有图形中。除了花时间修补
matplotlib
和添加
rc
对所需选项的支持之外,我想不出一种更简单的方法来修改大量图形

要使matplotlib不绘制上部和右侧脊椎,可以在matplotlibrc文件中设置以下内容:

axes.spines.right : False
axes.spines.top : False

还可以从matplotlibrc文件中删除顶部和右侧轴上的记号吗?@jkokorian:在即将发布的matplotlib 2.0.0版本中,可以设置
xtick.top
ytick.right
选项。
ax.spines["top"].set_visible(False)    
ax.spines["right"].set_visible(False)