【问题标题】:Change style in one Matplotlib Figure when working with several Figures simultaneously同时处理多个图形时更改一个 Matplotlib 图形的样式
【发布时间】:2018-07-03 13:55:34
【问题描述】:

假设我同时有 2 个 Matplotlib 图形,它们在 for 循环中更新。其中一个数字(假设fig0 有图像,而fig1 是线图)。我希望fig0fig1 中具有标准的Matplotlib 样式我想为fig1 设置plt.style.use('ggplot')

到目前为止,我已经尝试过:

plt.style.use('ggplot')

fig0 = plt.figure(0)
fig1 = plt.figure(1)

for i in range(10):
    # print stuff in both figures

但这会在两个图中设置ggplot 样式(如预期的那样)。我找不到在每个图中单独设置样式的方法。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这会解决它,除了循环。

    import matplotlib.pyplot as plt
    
    with plt.style.context('ggplot'):
        plt.figure(0)
        plt.plot([3,2,1])
    with plt.style.context('default'):
        plt.figure(1)
        plt.plot([1,2,3])
    
    plt.show()
    

    无论如何,如果没有循环,你可能会更好......只要循环由于某种原因不是绝对必要的。只需将您添加到循环中的图表中的任何内容保留在列表中并修改上面的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      相关资源
      最近更新 更多