对于函数中的循环以生成Barchart类的频率?

发布于 2025-02-10 03:14:56 字数 1173 浏览 0 评论 0原文

class Visualizer:
    def __init__(self, dataframe):
        self.df = dataframe
       
    def plot_bar(self, x,height, width, title = "  ",xlabel = "  ", ylabel = "  ",color = "   ", **kwargs):
        fig, ax = plt.subplots(figsize=(11, 9))
        
        if type(x) == type("xyz"):
            x = self.df[x].value_counts().index
       
            
        plt.bar(x= x,
                height= height,
                width = width,
                color= color,
                alpha=0.5)

        plt.xlabel(xlabel, fontsize = 20)
        plt.ylabel(ylabel, fontsize = 20)
        plt.xticks(fontsize = 16)
        plt.yticks(fontsize = 16)
    
        leg = plt.legend(loc='upper right', fancybox=True)
        leg.get_frame().set_alpha(0.5)
        plt.title(title, fontsize = 20)
        plt.show()

我称此函数为下面:

vizu =visualization("df")
vizu.plot_bar("State",200, 0.25, " Area vs State", "Area","Rentalprice",'r' )

如何拥有一个循环,该循环在函数中生成列(状态)中的类频率? 另外,我将输出的屏幕截图作为参考。

“在此处输入图像说明”

这是我的第一个堆栈流问题。任何错误,请接受我的道歉!

class Visualizer:
    def __init__(self, dataframe):
        self.df = dataframe
       
    def plot_bar(self, x,height, width, title = "  ",xlabel = "  ", ylabel = "  ",color = "   ", **kwargs):
        fig, ax = plt.subplots(figsize=(11, 9))
        
        if type(x) == type("xyz"):
            x = self.df[x].value_counts().index
       
            
        plt.bar(x= x,
                height= height,
                width = width,
                color= color,
                alpha=0.5)

        plt.xlabel(xlabel, fontsize = 20)
        plt.ylabel(ylabel, fontsize = 20)
        plt.xticks(fontsize = 16)
        plt.yticks(fontsize = 16)
    
        leg = plt.legend(loc='upper right', fancybox=True)
        leg.get_frame().set_alpha(0.5)
        plt.title(title, fontsize = 20)
        plt.show()

I call this function as below:

vizu =visualization("df")
vizu.plot_bar("State",200, 0.25, " Area vs State", "Area","Rentalprice",'r' )

How to have a loop which generates the frequency of classes in a column(state) within the function?
Also, I am attaching the screen shot of the output as the reference.

enter image description here

This is my 1st stack flow question. Any mistake, please accept my apologizes!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文