Почему Holoviz Panel показывает текст, а не сюжет о морском? - PullRequest
0 голосов
/ 06 января 2020

Я хочу создать панель Holoviz в блокноте Jupyter Notebook, содержащую график с полосами морского зайца. Я могу заставить приборную панель успешно отображать сюжеты matplotlib, но сюжет seaborn не отображается - только некоторый текст (AxesSubplot(0.125,0.125;0.775x0.755)).

Я смотрел некоторые примеры на сайте Holoviz и искал seaborn Speci c примеров, но не может найти ни одного. Я также искал StackOverflow и Google и не нашел ничего, что показывало бы мне, как успешно отобразить заговор моря.

Мой код:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import panel as pn
import hvplot as hv

# create a data set of animal ratings
df = pd.DataFrame({'Animal':['Pig', 'Goat' ,'Sheep', 'Frog', 'Goat', 'Goat', 'Pig', 'Sheep', 'Octopus'], 
                   'Rating':[3, 10, 3, 2, 9, 10, 4, 1, 1]})

# define the holoviz panel parameter selector and plots in a class
class RatingsDashboard(param.Parameterized):

    # widget containing the list of animals
    Animal = param.ObjectSelector(default='Goat', objects=list(df.Animal.unique()))

    title = 'Ratings for '
    xlabel = 'Rating'
    ylim = (0, 3)

    def get_data(self):
        class_df = df[(df.Animal==self.Animal)].copy()
        return class_df

    def hist_view_all(self):
        plot = plt.figure()
        plot.add_subplot(111).hist(df['Rating'])
        plt.close(plot)
        return plot

    # seaborn strip plot for all ratings for all animals
    def strip_view_all(self):
        plot = sns.stripplot(data = df, x='Animal', y='Rating', jitter=False, size=10)
        return plot

    def hist_view(self):
        data = self.get_data()
        title = "Histogram: " + self.title

        plot = plt.figure()
        plot.add_subplot(111).hist(data['Rating'])
        plt.title('Histogram of ' + self.title + self.Animal, size=14)
        plt.xlabel(self.xlabel, size=14)
        plt.xticks(size=12)
        plt.yticks(size=12)
        plt.ylim(self.ylim)

        plt.close(plot)
        return plot

    def table_view(self):
        data = self.get_data()
        return data

# create an instance of the class
rd = RatingsDashboard(name='')

# define the dashboard elements using a subset of the rd class plots
dashboard3 = pn.Column('## Animal Ratings', rd.strip_view_all, rd.param,
          pn.Row(rd.hist_view, rd.table_view))

# display the dashboard
dashboard3

Вывод: panel dashboard output

Вывод Seaborn Strip Plot, который должен отображаться вместо текста: enter image description here

1 Ответ

1 голос
/ 06 января 2020

Спасибо пользователю ImportanceOfBeingErnest за ответ на этот вопрос:

hist_view_all возвращает фигуру, а strip_view_all возвращает оси. Переменные с лучшим именем в соответствии с тем, что они содержат, ax = sns.stripplot (...) и возвращают цифру, возвращают ax.figure

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...