是否可以在保存图形时临时更改其style sheet,而无需重新打印(两次)?我知道您可以将图as parameters的facecolor
和edgecolor
传递给savefig
,但我不理解为什么它仅限于这两个参数。您似乎对axes
实例的rcParams
没有任何影响。Matplotlib的样式表上下文管理器只影响图形的样式表。下面是一个可以使用的MWE:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot((1, 2), (0, 0), '--')
with plt.style.context('dark_background'):
# if you replot it, it works, however, this messses up the style outside
# this context as well
# fig.clf()
# ax = fig.add_subplot(111)
# ax.plot((1, 2), (0, 0), '--')
fig.savefig('test.png')
# you would need to replot everything again for the style to be correct again
# fig.clf()
# ax = fig.add_subplot(111)
# ax.plot((1, 2), (0, 0), '--')
fig.show()
好的,经过大量的研究和反复试验,我终于找到了一种可行的方法,虽然很复杂。基本思想是编写您自己的(“智能”)上下文管理器,该管理器在抽象级别上理解您想要做什么,然后进行适当的更改。例如,如果您想更改}的},它知道哪些颜色更改与之相关
axes
{figure
的axes
{在更改
edgecolor
的情况下,有许多障碍需要注意,因为更改关联艺术家的(边缘)颜色对于不同艺术家的效果不同。以下是我遇到的一些警告,当然这个列表并不全面,因为我对更复杂的数字很感兴趣,还有其他一些事情需要考虑:axes
实例的facecolor
,但由于某些原因,edgecolor
没有等效的方法。因此,您需要手工操作并收集所有spine
、label
和tick
实例,并分别更改它们的颜色李>color
、facecolor
以及edgecolor
关键字,甚至有一个^{get_color()
方法。相反,您需要调用get_edgecolor()
李>colorbar
,你必须找到另一种方法,因为如果你想改变颜色栏的脊椎颜色,你不是通过改变脊椎的颜色来实现的,but by changing the color of the ^{我相信有很好的理由解释为什么事情会这样,但是从想要改变edgecolor的简单想法来看,这似乎是不必要的武断
下面是应用于一个简单示例图的上下文管理器的代码。我相信有很多方法可以改进这种方法
相关问题 更多 >
编程相关推荐