简体   繁体   English

plt.show() 不打印 plt.plot 仅 plt.scatter

[英]plt.show() does not print plt.plot only plt.scatter

So, for the following code no graph is printed in jupyter notebook.因此,对于以下代码,jupyter 笔记本中不会打印任何图形。 If I use plt.scatter then it does produce graph.如果我使用 plt.scatter 那么它确实会生成图表。 Any suggestions what could be wrong?有什么建议可能是错的吗? Can it be caused by the data?会不会是数据造成的?

import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt


def calc_gauss(df, index):

    x=df.iloc[[index]]
    mean = df.apply(lambda x: x.mean(), axis=1)
    mu=mean.iloc[[index]]
    std = df.std(axis=1)
    sig=std.iloc[[index]]

    dens = norm.pdf(x,mu,sig)

    # build the plot
    fig, ax = plt.subplots(figsize=(9,6))
    plt.style.use('fivethirtyeight')
    ax.plot(x, dens)
    return plt.show()

calc_gauss(df_distance, 339)

Instead of代替

return plt.show()

use利用

fig.show()

If you want the picture to show in the notebook, use %matplotlib inline in a cell evaluated before the show command如果要在笔记本中显示图片,请在show命令之前评估的单元格中使用%matplotlib inline

Note the problem was that the arrays were shape (1,24).请注意,问题是 arrays 的形状为 (1,24)。 plot likes only 1D arrays. plot 只喜欢 1D arrays。 Replacing ax.plot(x, dens) with ax.plot(x.reshape(-1), dens.reshape(-1)) solved the issue.ax.plot(x.reshape(-1), dens.reshape(-1))替换ax.plot(x, dens) ) 解决了这个问题。

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

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