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

2024-06-24 12:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,对于以下代码,jupyter笔记本中没有打印任何图形。如果我使用plt.scatter,那么它会生成图形。有什么建议吗?有什么问题吗?这可能是由数据引起的吗

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)

Tags: import图形dfindexmatplotlibascalcplt
1条回答
网友
1楼 · 发布于 2024-06-24 12:30:35

而不是

return plt.show()

使用

fig.show()

如果要在笔记本中显示图片,请在show命令之前计算的单元格中使用%matplotlib inline

注意问题在于阵列的形状(1,24)。plot只喜欢1D数组。用ax.plot(x.reshape(-1), dens.reshape(-1))替换ax.plot(x, dens)解决了这个问题

相关问题 更多 >