【问题标题】:Setting axes.linewidth without changing the rcParams global dict在不更改 rcParams 全局字典的情况下设置 axes.linewidth
【发布时间】:2011-02-02 22:56:05
【问题描述】:

因此,似乎无法执行以下操作(它会引发错误,因为axes 没有set_linewidth 方法):

axes_style = {'linewidth':5}
axes_rect = [0.1, 0.1, 0.9, 0.9]

axes(axes_rect, **axes_style)

并且必须使用以下旧技巧:

rcParams['axes.linewidth'] = 5 # set the value globally

... # some code

rcdefaults() # restore [global] defaults

有没有一种简单/干净的方法(可能是可以单独设置x- 和y- 轴参数等)?

如果不是,为什么?

【问题讨论】:

    标签: python matplotlib plot graphing


    【解决方案1】:

    是的,有一种简单而干净的方法可以做到这一点。

    从轴实例调用“axhline”和“axvline”似乎是 MPL 文档中认可的技术。

    无论如何,它都很简单,让您可以对坐标区的外观进行细粒度控制。

    例如,此代码将创建一个绘图并将 x 轴着色为绿色,并将 x 轴的线宽从默认值“1”增加到值“4”; y轴为红色,y轴的线宽从“1”增加到“8”。

    from matplotlib import pyplot as PLT
    fig = PLT.figure()
    ax1 = fig.add_subplot(111)
    
    ax1.axhline(linewidth=4, color="g")        # inc. width of x-axis and color it green
    ax1.axvline(linewidth=4, color="r")        # inc. width of y-axis and color it red
    
    PLT.show()
    

    axhline/axvline 函数接受额外的参数,这些参数应该允许你在美学上做任何你想做的事情,特别是 ~matplotlib.lines.Line2D 的任何属性都是有效的 kwargs(例如,'alpha'、'linestyle' , capstyle, joinstyle)。

    【讨论】:

    • 非常感谢,我会在几个小时后仔细查看。
    • 那是MC外语培训,顺便说一句? :)
    • 至少在最新版本的matplotlib中,这段代码在y=0创建了一条水平线,在x=0创建了一条垂直线。这与更改轴的颜色/厚度不同。
    • @tiago 所以它被有效地破坏了,或者没有?
    • 这个答案不清楚。这不会改变轴的厚度(如暗示的那样),它只是在默认的“0”位置覆盖水平和垂直彩色线。答案错过了在将日期时间轴绘制为“0”时相当重要的位置参数是 1970-01-01。 ax.spines 答案是关于 OP 问题的更好答案
    【解决方案2】:
    • 这个answer 不起作用,正如 cmets 中所解释的那样。我建议使用spines
    • 正如 Czechnology 的评论中提到的,也可以考虑更改刻度。
    import matplotlib.pyplot as plt
    
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
    
    ax1.set_title('Normal spine and ticks')
    ax2.set_title('Adjusted spine and ticks')
    
    # change each spine separately:
    # ax.spines['right'].set_linewidth(0.5)
    
    # change all spines
    for axis in ['top','bottom','left','right']:
        ax2.spines[axis].set_linewidth(4)
    
    # increase tick width
    ax2.tick_params(width=4)
    
    plt.show()
    

    【讨论】:

      【解决方案3】:
      plt.setp(ax.spines.values(), linewidth=5)
      

      【讨论】:

        【解决方案4】:

        如果您使用 pyplot 递归创建(非矩形)轴,则可以更改每个轴的线宽参数。

        例如:

        import matplotlib.pyplot as plt
        
        plt.figure(figsize = figsize)
        fig, ax = plt.subplots(figsize = figsize)
        for shape in sf.shapeRecords():
            x = [i[0] for i in shape.shape.points[:]]
            y = [i[1] for i in shape.shape.points[:]]
            ax.plot(x, y, 'k', linewidth=5)
        

        有关文档,请参阅MPL.axes documentation(向下滚动直到“其他参数”-> **kwargs)

        注意“如果您使用一个绘图命令制作多行,则 kwargs 适用于所有这些行。”

        也许此解决方案与其他地方提出的不同问题有关,但我发现此页面正在寻找解决我自己问题的方法,因此也许它可以帮助其他人寻找相同的问题。

        【讨论】:

          猜你喜欢
          • 2016-02-16
          • 1970-01-01
          • 2019-12-09
          • 2022-12-10
          • 2019-11-14
          • 2021-01-31
          • 1970-01-01
          • 1970-01-01
          • 2019-12-14
          相关资源
          最近更新 更多