python - 更改 ggplot 的默认样式

标签 python ggplot2 styles decorator

我编写了以下装饰函数来更改每个绘图任务调用的 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

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

最佳答案

查看引用文档here您可以更改以下内容来实现您想要的效果:

  • 面板背景
  • panel.grid.major
  • panel.grid.minor

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

panel.background = element_rect(fill = "white", colour = "grey50") Image from reference documents to set the background of a grid to white

要修改您的装饰器,我会将它们添加到 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

关于python - 更改 ggplot 的默认样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211230/

相关文章:

python - HyperlinkedIdentityField + @detail_route

r - 内插空间数据

html - 如何在 Mobile View 的总屏幕上显示 sideTab?

css - IE8 的样式问题

python - 准确度表现良好,但图像分割中的 Dice 损失较差

jquery - 从python文件中的ajax发布请求获取发布数据

Python - 替换部分 C 程序,但跳过注释

r - 创建半 donut 或议会席位图表

减少 R 中 ggplot 的主轴和 CI

wpf - WPF MVVM-TreeView中项目的样式丢失