在 matplotlib 中优雅地改变图框的颜色 [英] Elegantly changing the color of a plot frame in matplotlib

查看:39
本文介绍了在 matplotlib 中优雅地改变图框的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是讨论;全局变化,而不是局部变化.我想要一些其他不同颜色的图.请不要讨论绘图中的太多颜色... :-)

matplotlib.rc('axes',edgecolor='green')

  • 挖出轴的刺,然后改变它:也讨论过这里;我觉得不是很优雅.

     for child in ax.get_children():如果是实例(孩子,matplotlib.spines.Spine):child.set_color('#dddddd')

  • <块引用>

    有没有一种优雅的方式来压缩上面的块,一些东西更pythonic"?

    我在 ubuntu 下使用 python 2.6.5 和 matplotlib 0.99.1.1.

    解决方案

    重构上面的代码:

    将 matplotlib.pyplot 导入为 plt对于 ax,zip 中的颜色([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']):plt.setp(ax.spines.values(),颜色=颜色)plt.setp([ax.get_xticklines(), ax.get_yticklines()], 颜色=颜色)

    This is a kind of follow-up question to this post, where the coloring of axes, ticks and labels was discussed. I hope it is alright to open a new, extended question for this.

    Changing the color of a complete frame (ticks and axes) around a double-plot (via add_subplot) with axes [ax1, ax2] results in a lot of code. This snippet changes the color of the frame of the upper plot:

    ax1.spines['bottom'].set_color('green')
    ax1.spines['top'].set_color('green')
    ax1.spines['left'].set_color('green')
    ax1.spines['right'].set_color('green')
    for t in ax1.xaxis.get_ticklines(): t.set_color('green')
    for t in ax1.yaxis.get_ticklines(): t.set_color('green')
    for t in ax2.xaxis.get_ticklines(): t.set_color('green')
    for t in ax2.yaxis.get_ticklines(): t.set_color('green')
    

    So for changing the frame color of two plots with two y-axes each, I would need 16(!) lines of code... This is how it looks like:

    Other methods I dug up so far:

    • matplotlib.rc: discussed here; changes globally, not locally. I want to have some other plots in different colors. Please, no discussions about too many colors in plots... :-)

      matplotlib.rc('axes',edgecolor='green')
      

    • dig out the spines of the axis, then change it: also discussed here; not really elegant, I think.

      for child in ax.get_children():
          if isinstance(child, matplotlib.spines.Spine):
              child.set_color('#dddddd')
      

    Is there an elegant way of condensing the above block, something more "pythonic"?

    I'm using python 2.6.5 with matplotlib 0.99.1.1 under ubuntu.

    解决方案

    Refactoring your code above:

    import matplotlib.pyplot as plt
    
    for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']):
        plt.setp(ax.spines.values(), color=color)
        plt.setp([ax.get_xticklines(), ax.get_yticklines()], color=color)
    

    这篇关于在 matplotlib 中优雅地改变图框的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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