具有不同样式的多个 savefig()
multiple savefig() 's with different styles
我正在编写一个程序,它有两个输出:一个 GUI 和一个打印在纸上的报告(一个简单的 pdf 打印出来)。
我希望在两个输出上都有一个图表,但样式不同。
- dark_background 在 GUI 上 (http://matplotlib.org/examples/style_sheets/plot_dark_background.html)
- 和纸上的 fivethirtyeight http://matplotlib.org/examples/style_sheets/plot_fivethirtyeight.html
不知何故,我无法生成 2 张具有适当样式的图像。其中只有一个是正确的。我没有足够的经验值来 post 图片。所以我将 post 只有我的代码。
我的第一个想法是:
import numpy as np
import matplotlib.pyplot as plt
def plot():
#set pic size
fig = plt.figure(figsize=(16, 9), dpi=100)
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [10,9,8,7,6,5,4,3,2,1]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.1, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,1.3))
# Labels
ax.set_xlabel('points number')
ax.set_ylabel('values')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
plt.style.use('dark_background')
plt.savefig('result_dark.png')
plt.style.use('fivethirtyeight')
plt.savefig('result_white.png')
但是没有正常工作。其中一张图片是正确的。第二个有正确的背景颜色,但 legend/labels 的字体颜色没有改变。我试着把两张图片分开,结果是一样的:
import numpy as np
import matplotlib.pyplot as plt
import os
def plot():
#set pic size
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [1,2,3,1,2,3,1,2,3,1]
y3 = [3,1,2,3,1,2,3,1,2,3]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.2, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,5.3))
# Labels
ax.set_xlabel('Messpunkte-Nr.\nMeasurement points number')
ax.set_ylabel('Spezifikationsgrenze normiert\nnormed to specification')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
texts =legend.get_texts()
texts[0].set_color('green')
texts[1].set_color('orange')
texts[2].set_color('purple')
fig = plt.figure(figsize=(16, 9), dpi=100)
plt.style.use('dark_background')
plot()
plt.savefig('result_dark.png')
plt.clf()
#plt.close()
fig = plt.figure(figsize=(16, 9), dpi=100)
plt.style.use('fivethirtyeight')
plot()
plt.savefig('result_white.png')
plt.clf()
#plt.close()
我应该如何修复我的代码,使 2 张图像具有相同的值,但样式不同?
所以你的问题似乎是你做了很多绘图,然后告诉 pylab 你希望你的绘图具有特定的风格。该指令似乎并没有更新所有内容。所以相反,告诉它你想使用特定的样式。然后情节。然后清空一切。然后再画。
import numpy as np
import matplotlib.pyplot as plt
def plot():
#set pic size
fig = plt.figure(figsize=(16, 9), dpi=100)
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [10,9,8,7,6,5,4,3,2,1]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.1, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,1.3))
# Labels
ax.set_xlabel('points number')
ax.set_ylabel('values')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
plt.style.use('dark_background')
plot()
plt.savefig('result_dark.png')
plt.clf()
plt.style.use('fivethirtyeight')
plot()
plt.savefig('result_white.png')
这能满足您的要求吗?这是我得到的数据。
我建议您编写如下代码:
from matplotlib.style import context
def my_plot_function(ax, data, style):
# do all of your plotting in here, should be fast as no computation
pass
with context('dark'):
fig, ax = plt.subplots(1, 1)
my_plot_function(ax, data, style)
fig.savefig(...)
with context('fivethirtyeight'):
fig, ax = plt.subplots(1, 1)
my_plot_function(ax, data, style)
fig.savefig(...)
这是一项设计功能,不是错误。几乎所有由 rcparams 控制的值都是在对象创建时设置的,而不是在绘制时设置的,因为当你渲染它时你的图形看起来像什么取决于全局状态是可怕的。这还允许您为 rcparams 使用上下文管理器,如上所示。调用 use
只会覆盖样式 sheet 显式设置的值(这也是一个设计功能,因此您可以应用多个样式 a-la 级联样式 sheets)。
我正在编写一个程序,它有两个输出:一个 GUI 和一个打印在纸上的报告(一个简单的 pdf 打印出来)。
我希望在两个输出上都有一个图表,但样式不同。
- dark_background 在 GUI 上 (http://matplotlib.org/examples/style_sheets/plot_dark_background.html)
- 和纸上的 fivethirtyeight http://matplotlib.org/examples/style_sheets/plot_fivethirtyeight.html
不知何故,我无法生成 2 张具有适当样式的图像。其中只有一个是正确的。我没有足够的经验值来 post 图片。所以我将 post 只有我的代码。
我的第一个想法是:
import numpy as np
import matplotlib.pyplot as plt
def plot():
#set pic size
fig = plt.figure(figsize=(16, 9), dpi=100)
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [10,9,8,7,6,5,4,3,2,1]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.1, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,1.3))
# Labels
ax.set_xlabel('points number')
ax.set_ylabel('values')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
plt.style.use('dark_background')
plt.savefig('result_dark.png')
plt.style.use('fivethirtyeight')
plt.savefig('result_white.png')
但是没有正常工作。其中一张图片是正确的。第二个有正确的背景颜色,但 legend/labels 的字体颜色没有改变。我试着把两张图片分开,结果是一样的:
import numpy as np
import matplotlib.pyplot as plt
import os
def plot():
#set pic size
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [1,2,3,1,2,3,1,2,3,1]
y3 = [3,1,2,3,1,2,3,1,2,3]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.2, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,5.3))
# Labels
ax.set_xlabel('Messpunkte-Nr.\nMeasurement points number')
ax.set_ylabel('Spezifikationsgrenze normiert\nnormed to specification')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
texts =legend.get_texts()
texts[0].set_color('green')
texts[1].set_color('orange')
texts[2].set_color('purple')
fig = plt.figure(figsize=(16, 9), dpi=100)
plt.style.use('dark_background')
plot()
plt.savefig('result_dark.png')
plt.clf()
#plt.close()
fig = plt.figure(figsize=(16, 9), dpi=100)
plt.style.use('fivethirtyeight')
plot()
plt.savefig('result_white.png')
plt.clf()
#plt.close()
我应该如何修复我的代码,使 2 张图像具有相同的值,但样式不同?
所以你的问题似乎是你做了很多绘图,然后告诉 pylab 你希望你的绘图具有特定的风格。该指令似乎并没有更新所有内容。所以相反,告诉它你想使用特定的样式。然后情节。然后清空一切。然后再画。
import numpy as np
import matplotlib.pyplot as plt
def plot():
#set pic size
fig = plt.figure(figsize=(16, 9), dpi=100)
ax = plt.subplot(111)
# set x data
x = range(10)
# set y data
y1 = np.zeros(10)
y2 = [0,1,2,3,4,5,6,7,8,9]
y3 = [10,9,8,7,6,5,4,3,2,1]
#plot as errorbar
ax.errorbar(x, y1, fmt='o', color='green', markersize=8, label='Normal')
ax.errorbar(x, y2, yerr=0.1, fmt='o', color='orange', markersize=8, label='abw_up')
ax.errorbar(x, y3, yerr=0.1, fmt='o', color='purple', markersize=8,label='abw_down')
# limits
ax.axhline(0.1*10, color='red', lw=2)
ax.axhline(-0.1*10, color='red', lw=2)
#set limit of y-Axis
ax.set_ylim((-1.3,1.3))
# Labels
ax.set_xlabel('points number')
ax.set_ylabel('values')
# legend
legend=ax.legend(loc=('upper center'), shadow='true',bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True)
plt.style.use('dark_background')
plot()
plt.savefig('result_dark.png')
plt.clf()
plt.style.use('fivethirtyeight')
plot()
plt.savefig('result_white.png')
这能满足您的要求吗?这是我得到的数据。
我建议您编写如下代码:
from matplotlib.style import context
def my_plot_function(ax, data, style):
# do all of your plotting in here, should be fast as no computation
pass
with context('dark'):
fig, ax = plt.subplots(1, 1)
my_plot_function(ax, data, style)
fig.savefig(...)
with context('fivethirtyeight'):
fig, ax = plt.subplots(1, 1)
my_plot_function(ax, data, style)
fig.savefig(...)
这是一项设计功能,不是错误。几乎所有由 rcparams 控制的值都是在对象创建时设置的,而不是在绘制时设置的,因为当你渲染它时你的图形看起来像什么取决于全局状态是可怕的。这还允许您为 rcparams 使用上下文管理器,如上所示。调用 use
只会覆盖样式 sheet 显式设置的值(这也是一个设计功能,因此您可以应用多个样式 a-la 级联样式 sheets)。