【问题标题】:Data not appearing on a python plot数据未出现在 python 图上
【发布时间】:2018-08-08 07:33:55
【问题描述】:

我有一个数据框,其中日期为索引,浮点数为列,主要填充 NaN 和一些浮点数。

我正在绘制这个数据框:

fig, ax = plt.subplots()
plot(df2[11][2:], linestyle='dashed',linewidth=2,label='xx')
ax.set(xlabel='xx', ylabel='xx', title='xx')
ax.grid()
ax.legend()

绘图窗口打开,但没有出现数据。但是如果我使用标记而不是线,就会出现数据点。

我应该更正什么以将我的图表绘制为线?

编辑谢谢,它是这样工作的:

s1 = np.isfinite(df2[11][2:])
fig, ax = plt.subplots()
plot(df2.index[2:][s1],df2[11][2:].values[s1], linestyle='-',linewidth=2,label='xx')
ax.set(xlabel='xx', ylabel='xx',title='xx')
ax.grid()
ax.legend()

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    试试

    import matplotlib.pyplot as plt
    fig = plt.figure()
    plt.plot(df2[11][2:], linestyle='dashed',linewidth=2,label='xx')
    plt.set(xlabel='xx', ylabel='xx', title='xx')
    plt.grid()
    plt.legend()
    plt.show()
    

    【讨论】:

      【解决方案2】:

      在您的情况下,matplotlib 不会在由 NaN 分隔的点之间画线。您可以屏蔽 NaN 或摆脱它们。看看下面的链接,有一些解决方案可以绘制跳过 NaN 的线。

      matplotlib: drawing lines between points ignoring missing data

      【讨论】:

        猜你喜欢
        • 2021-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多