简体   繁体   中英

plt.show() does not print plt.plot only plt.scatter

So, for the following code no graph is printed in jupyter notebook. If I use plt.scatter then it does produce graph. 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

Note the problem was that the arrays were shape (1,24). plot likes only 1D arrays. Replacing ax.plot(x, dens) with ax.plot(x.reshape(-1), dens.reshape(-1)) solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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