Holoviz Panel为什么显示文本而不是seaborn绘图?

huangapple go评论87阅读模式
英文:

Why is Holoviz Panel showing text instead of seaborn plot?

问题

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

我已经查看了Holoviz网站上的一些示例,并搜索了seaborn的特定示例,但没有找到任何相关信息。我还在StackOverflow和Google上搜索,但没有找到任何可以成功显示seaborn绘图的示例。

我的代码:

import param
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

# 创建一个动物评分数据集
df = pd.DataFrame({'Animal':['Pig', 'Goat', 'Sheep', 'Frog', 'Goat', 'Goat', 'Pig', 'Sheep', 'Octopus'], 
                   'Rating':[3, 10, 3, 2, 9, 10, 4, 1, 1]})

# 在一个类中定义Holoviz Panel参数选择器和绘图
class RatingsDashboard(param.Parameterized):
    
    # 包含动物列表的小部件
    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条带图
    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

# 创建一个类的实例
rd = RatingsDashboard(name='')

# 使用rd类绘图的子集来定义仪表板元素
dashboard3 = pn.Column('## 动物评分', rd.strip_view_all, rd.param,
          pn.Row(rd.hist_view, rd.table_view))

# 显示仪表板
dashboard3

输出:
Holoviz Panel为什么显示文本而不是seaborn绘图?

应显示的Seaborn条带图输出而不是文本:
Holoviz Panel为什么显示文本而不是seaborn绘图?

英文:

I want to create a Holoviz Panel dashboard in a Jupyter Notebook, containing a seaborn strip plot. I can get the dashboard to display matplotlib plots successfully but the seaborn plot is not displayed - just some text (AxesSubplot(0.125,0.125;0.775x0.755)).

I've looked at some examples on the Holoviz website and searched for seaborn specific examples but can't find any. I've also searched StackOverflow and Google and have not found anything that shows me how to successfully display a seaborn plot.

My code:

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

Output:
Holoviz Panel为什么显示文本而不是seaborn绘图?

Seaborn Strip Plot output that should be displayed instead of the text:
Holoviz Panel为什么显示文本而不是seaborn绘图?

答案1

得分: 2

感谢用户ImportanceOfBeingErnest提供答案:

> hist_view_all返回一个图形,而strip_view_all返回一个轴。根据它们包含的内容来命名变量,ax = sns.stripplot(...),然后返回图形,返回ax.figure。

英文:

Thanks to user ImportanceOfBeingErnest for answering this:

> hist_view_all returns a figure, while strip_view_all returns an axes. Best name variables according to what they contain, ax = sns.stripplot(...), and return the figure, return ax.figure

huangapple
  • 本文由 发表于 2020年1月6日 18:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定