【问题标题】:How do i create a series that I can create a bar chart from我如何创建一个可以从中创建条形图的系列
【发布时间】:2019-05-18 12:39:49
【问题描述】:

我有一个名字列表:['joe','brian','stephen']

我想在 jupyter 中创建一个条形图,其中名称作为索引,条形图显示每个名称中的字母数量。如何创建一个可以调用“.plot(kind='bar')”的系列并获得描述的条形图?

【问题讨论】:

    标签: python matplotlib bar-chart


    【解决方案1】:
    import numpy as np
    import matplotlib.pyplot as plt
    
    bars = ['joe','brian','stephen']
    height = [len(m) for m in bars]
    y_pos = np.arange(len(bars))
    plt.bar(y_pos, height, color=(0.2, 0.4, 0.6, 0.6))
    plt.xticks(y_pos, bars)
    plt.show()
    

    【讨论】:

      【解决方案2】:

      您可以使用您的名称列表创建一个数据框,然后应用len 函数。

      import pandas as pd
      import matplotlib.pyplot as plt
      
      df = pd.DataFrame({'names':['joe','brian','stephen']},dtype=str)
      df['name_len'] = df['names'].apply(len)
      
      df.plot(kind='bar',x='names')
      plt.show()
      

      【讨论】:

        【解决方案3】:

        你可以试试这个,

        import matplotlib.pyplot as plt
        
        x =  ['joe','brian','stephen']
        y = [len(m) for m in x]
        
        plt.bar(x, y)
        plt.show()
        

        【讨论】:

        • 如果您不想装扮颜色、位置等,也可以试试这个答案。
        猜你喜欢
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        相关资源
        最近更新 更多