【问题标题】:Changing the default style for ggplot更改 ggplot 的默认样式
【发布时间】:2017-07-20 09:50:18
【问题描述】:

我编写了以下装饰函数来更改每个绘图任务调用 ggplot 的默认样式:

# Style plots (decorator)
def plot_styling(func):
    def wrapper(*args, **kwargs):
        style = {'axes.titlesize' : 24,
                'axes.labelsize' : 20,
                'lines.linewidth' : 3,
                'lines.markersize' : 10,
                'xtick.labelsize' : 16,
                'ytick.labelsize' : 16,
                }
        with plt.style.context((style)):
            ax = func(*args, **kwargs)      
    return wrapper

现在,我想将默认网格颜色从浅灰色更改为白色,并将网格线更改为灰色。我该怎么办?

【问题讨论】:

    标签: python ggplot2 styles decorator


    【解决方案1】:

    查看参考文档here,您可以更改以下内容以实现您想要的:

    • panel.background
    • panel.grid.major
    • panel.grid.minor

    这里的引用意味着我们将这些属性的值分配给从element_ 函数返回的值。

    panel.background = element_rect(fill = "white", colour = "grey50")

    要修改您的装饰器,我会将这些添加到 style 字典中。

    # Style plots (decorator)
    def plot_styling(func):
        def wrapper(*args, **kwargs):
            style = {'axes.titlesize': 24,
                     'axes.labelsize': 20,
                     'lines.linewidth': 3,
                     'lines.markersize': 10,
                     'xtick.labelsize': 16,
                     'ytick.labelsize': 16,
                     'panel.background': element_rect(fill="white"),
                     'panel.grid.major': element_line(colour="grey50"),
                     'panel.grid.minor': element_line(colour="grey50")
                    }
            with plt.style.context((style)):
                ax = func(*args, **kwargs)      
        return wrapper
    

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 2023-03-30
      • 2020-10-02
      • 2016-12-12
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多