【问题标题】:Why is Holoviz Panel showing text instead of seaborn plot?为什么 Holoviz Panel 显示文本而不是 seaborn 情节?
【发布时间】:2020-04-23 21:40:45
【问题描述】:

我想在 Jupyter Notebook 中创建一个 Holoviz Panel 仪表板,其中包含 seaborn 条形图。我可以让仪表板成功显示 matplotlib 图,但 seaborn 图未显示 - 只是一些文本 (AxesSubplot(0.125,0.125;0.775x0.755))。

我查看了 Holoviz 网站上的一些示例并搜索了 seaborn 特定示例,但找不到任何示例。我还搜索了 StackOverflow 和 Google,但没有找到任何可以告诉我如何成功显示 seaborn 绘图的内容。

我的代码:

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

输出:

应该显示而不是文本的Seaborn Strip Plot 输出:

【问题讨论】:

  • hist_view_all 返回一个图形,而 strip_view_all 返回一个坐标区。根据变量包含的内容命名最佳变量ax = sns.stripplot(...),并返回数字return ax.figure
  • 谢谢!那行得通!你是超级明星 :) 我会阅读更多关于 fig 和 ax 以避免类似问题的信息。

标签: python seaborn panel dashboard holoviz


【解决方案1】:

感谢用户ImportanceOfBeingErnest回答这个问题:

hist_view_all 返回一个图形,而 strip_view_all 返回一个轴。最好根据它们包含的变量命名,ax = sns.stripplot(...),并返回图,返回ax.figure

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 2011-11-21
    • 2018-11-27
    相关资源
    最近更新 更多