gpt4 book ai didi

python - matplotlib 中图形的意外叠加

转载 作者:行者123 更新时间:2023-11-28 17:29:23 25 4
gpt4 key购买 nike

enter image description here enter image description here

看起来第一个图中的数据点不小心覆盖了第二个图。我正在运行的代码被运行了几次,当我第一次运行它时,它的周期很短,第二次运行它时,它的周期较长,而短期内的数据点也是较长周期的一部分。

那么有没有一种方法可以在开始构建图表之前清理图表?

您可以在此处查看构建图表的代码:

def create_graph(self, device):

# 800 and 355 pixels.
ticks = 5
width = 8
height = 3.55

dpi = 100
bgcolor = '#f3f6f6'

font = {
'size': 16,
'family': 'Arial'
}
plt.rc('font', **font)

# size of figure and setting background color
fig = plt.gcf()
fig.set_size_inches(width, height)
fig.set_facecolor(bgcolor)

# axis color, no ticks and bottom line in grey color.
ax = plt.axes(axisbg=bgcolor, frameon=True)
ax.xaxis.set_ticks_position('none')
ax.spines['bottom'].set_color('#aabcc2')
ax.yaxis.set_ticks_position('none')

# removing all but bottom spines
for key, sp in ax.spines.items():
if key != 'bottom':
sp.set_visible(False)

# setting amounts of ticks on y axis
yloc = plt.MaxNLocator(ticks)
ax.yaxis.set_major_locator(yloc)


x_no_ticks = 8
# Deciding how many ticks we want on the graph
locator = AutoDateLocator(maxticks=x_no_ticks)
formatter = AutoDateFormatter(locator)
# Formatter always chooses the most granular since we have granular dates
# either change format or round dates depending on how granular
# we want them to be for different date ranges.
formatter.scaled[1/(24.*60.)] = '%d/%m %H:%M'

ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)

# turns off small ticks
plt.tick_params(axis='x',
which='both',
bottom='on',
top='off',
pad=10)
# Can't seem to set label color differently, changing tick_params color changes labels.
ax.xaxis.label.set_color('#FFFFFF')

# setting dates in x-axis automatically triggers use of AutoDateLocator
x = [datetime.fromtimestamp(point['x']) for point in device['data']]
y = [point['y'] for point in device['data']]
plt.plot(x, y, color='#53b4d4', linewidth=2)

# pick values for y-axis
y_ticks_values = np.array([point['y'] for point in device['data']])
y_ticks = np.linspace(y_ticks_values.min(), y_ticks_values.max(), ticks)
y_ticks = np.round(y_ticks, decimals=2)
plt.yticks(y_ticks, [str(val) + self.extract_unit(device) for val in y_ticks])
# plt.ylim(ymin=0.1) # Only show values of a certain threshold.

plt.tight_layout()
buf = io.BytesIO()
plt.savefig(buf,
format='png',
facecolor=fig.get_facecolor(),
dpi=dpi)

最佳答案

您必须在 plt.savefig() 之后添加 plt.close()。因此下一个 plt.gcf() 调用不会捕捉到该图形。

关于python - matplotlib 中图形的意外叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35629742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com