简体   繁体   English

matplotlib: plt.show() 上的第二个空 window

[英]matplotlib: Second empty window on plt.show()

I usually don't ask questions on this platform, but I have a problem that quite bugs me.我通常不会在这个平台上提问,但是我有一个问题让我很烦恼。

Context语境

I have a function that plots data from a dataframe that has stockdata.我有一个 function 绘制来自具有 stockdata 的 dataframe 的数据。 It all works perfectly except for the fact that a second, empty window shows next to the actual graph whenever I execute this function.除了当我执行此 function 时在实际图形旁边显示第二个空 window 之外,这一切都运行良好。 (image) (图片)

Here is all the relevant code, I'd be very grateful if some smart people could help me.这是所有相关代码,如果有聪明人可以帮助我,我将不胜感激。

    def plot(self):
    plt.clf()
    plt.cla()
    colors = Colors()
    data = self.getStockData()
    if data.empty:
        return
    data.index = [TimeData.fromTimestamp(x) for x in data.index]
    current, previous = data.iloc[-1, 1], data.iloc[0, 1]
    percentage = (current / previous - 1) * 100
    # Create a table
    color = colors.decideColorPct(percentage)
    # Create the table
    fig = plt.figure(edgecolor=colors.NEUTRAL_COLOR)
    fig.patch.set_facecolor(colors.BACKGROUND_COLOR)
    plt.plot(data.close, color=color)
    plt.title(self.__str2__(), color=colors.NEUTRAL_COLOR)
    plt.ylabel("Share price in $", color=colors.NEUTRAL_COLOR)
    plt.xlabel("Date", color=colors.NEUTRAL_COLOR)
    ax = plt.gca()

    ax.xaxis.set_major_formatter(plt_dates.DateFormatter('%Y/%m/%d %H:%M'))
    ax.set_xticks([data.index[0], data.index[-1]])
    ax.set_facecolor(colors.BACKGROUND_COLOR)
    ax.tick_params(color=colors.NEUTRAL_COLOR, labelcolor=colors.NEUTRAL_COLOR)
    for spine in ax.spines.values():
        spine.set_edgecolor(colors.NEUTRAL_COLOR)

    ax.yaxis.grid(True, color=colors.NEUTRAL_COLOR, linestyle=(0, (5, 10)), linewidth=.5)
    plt.show()

Some notes:一些注意事项:

Matplotlib never gets used in the program before this. Matplotlib 在此之前从未在程序中使用过。

The data is standardized and consists of the following columns: open, low, high, close, volume.数据是标准化的,由以下列组成:开盘价、最低价、最高价、收盘价、成交量。

The index of the dataframe exists of timestamps, which gets converted to an index of datetime objects at the following line: data.index = [TimeData.fromTimestamp(x) for x in data.index] dataframe 的索引存在时间戳,它在以下行转换为日期时间对象的索引: data.index = [TimeData.fromTimestamp(x) for x in data.index]

Remove plt.clf() and plt.cla() because it automatically creates window for plot when you don't have this window.删除plt.clf()plt.cla()因为当你没有这个 window 时,它会自动为 plot 创建 window。

And later fig = plt.figure() creates new window which it uses to display your plot.然后fig = plt.figure()创建新的 window 用于显示您的 plot。


Minimal code for test最少的测试代码

import matplotlib.pyplot as plt
import pandas as pd

data = pd.DataFrame({'x': [1,2,3], 'y': [2,3,1]})

#plt.clf()
#plt.cla()
fig = plt.figure()
plt.plot(data)
ax = plt.gca()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM