在matplotlibrc中设置刺 [英] Setting spines in matplotlibrc

查看:58
本文介绍了在matplotlibrc中设置刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于一个奇怪的原因,我找不到在Python的matplotlibrc文件中指定棘刺配置的方法.关于如何使matplotlib在默认情况下不绘制上下棘的任何想法?
(来源: sourceforge.net )

有关matplotlib中棘刺的更多信息,请此处

谢谢

解决方案

要隐藏子图的右棘和上棘,您需要将相关棘的颜色都设置为'none',并设置将刻度线的刻度位置设置为'left',将刻度线的刻度位置设置为'bottom'(以便隐藏刻度线和棘刺).

不幸的是,这些都当前无法通过matplotlibrc访问.验证matplotlibrc中指定的参数,然后将其存储在称为rcParams的字典中.然后由各个模块检查此字典中的键,其值将作为其默认值.如果他们没有选择其中一个选项,则该选项不能通过rc文件更改.

由于rc系统的性质以及编写刺的方式,更改代码以实现此目的并不容易:

当前,脊柱通过用于定义轴颜色的相同rc参数获得颜色.如果不隐藏所有轴图,则无法将其设置为'none'.他们也不知道它们是toprightleft还是bottom—这些实际上只是存储在字典中的四个单独的刺.各个脊柱对象不知道它们组成图的哪一侧,因此您不能只添加新的rc参数并在脊柱初始化期间分配适当的参数.

self.set_edgecolor( rcParams['axes.edgecolor'] )

( ./matplotlib/lib/matplotlib/spines.py ,__init __(),第54行)

如果您有大量的现有代码,以至于将轴参数手动添加到每个参数中太麻烦了,则可以选择使用辅助函数来遍历所有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_spines(),它将在show()显示的所有图形中将其隐藏.除了花费时间修补matplotlib并为所需的选项添加rc支持之外,我无法想到一种更简单的方法来更改大量图形.

For a strange reason I cannot find the way to specify spines configuration in Python's matplotlibrc file. Any idea on how to cause matplotlib not to draw upper and right spines by default?
(source: sourceforge.net)

More about info about spines in matplotlib is here

Thank you

解决方案

In order to hide the right and top spines of a subplot, you need to both set the colour of the relevant spines to 'none', as well as set the tick position to 'left' for the xtick, and 'bottom' for the ytick (in order to hide the tick marks as well as the spines).

Unfortunately, none of these are currently accessible via matplotlibrc. The parameters specified in matplotlibrc are validated, and then stored in a dict called rcParams. It is then up to the individual modules to check for a key in this dict whose value will act as their default. If they don't check it for one of their options, that option is not alterable via the rc file.

Due to the nature of the rc system, and the way that spines are written, altering the code to allow for this would not be straightforward:

Spines currently obtain their colour through the same rc parameter used to define axis colours; you cannot set it to 'none' without hiding all of your axis drawing. They are also agnostic towards whether they are top, right, left, or bottom — these are really just four separate spines stored in a dict. The individual spine objects do not know what side of the plot they compose, so you cannot just add new rc params and assign the proper one during spine initialization.

self.set_edgecolor( rcParams['axes.edgecolor'] )

(./matplotlib/lib/matplotlib/spines.py, __init__(), line 54)

If you have a large amount of existing code, such that adding the axis parameters manually to each one would be too burdensome, you could alternately use a helper function to iterate through all of the Axis objects and set the values for you.

Here's an example:

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

Just call hide_spines() before show(), and it will hide them in all of the figures that show() displays. I cannot think of a simpler way to alter a large number of figures, outside of spending the time to patch matplotlib and add in rc support for the needed options.

这篇关于在matplotlibrc中设置刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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