【问题标题】:Setup the width, size and all of the barchart (seaborn)设置条形图的宽度、大小和所有(seaborn)
【发布时间】:2021-01-23 18:05:18
【问题描述】:

我使用 seaborn 的 python 库开发了一个条形图。 这里的月份部分只有 3 个月,产品部分有很多产品,根据那个数量有 基本上我可以告诉你,当我打印这个数据框时,它总共给出了 979 行 并且每个月的产品可能在 300 左右,也可能没有 在条形图中,条形图非常混乱,我无法清楚地看到哪个产品在一个月内销量最高

代码如下:

import pandas as pd
import mysql.connector
import seaborn as sns
import matplotlib.pyplot as plt

db_connection = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  db="trial"
)

cursor = db_connection.cursor()

df = pd.read_sql("select ProductName,Month,Bottle from merge where Bottle>0",db_connection)

mon=[]
prod=[]
quan=[]

for i in df.itertuples():
    mon.append(i.Month)
    prod.append(i.ProductName)
    quan.append(i.Bottle)

dfd = pd.DataFrame({"Month":mon,"Product":prod,"Quantity":quan})

sns.barplot(x="Month",y="Quantity",hue="Product",data=dfd,palette="Set1")

plt.show()

这是执行此代码 sn-p 时出现的条形图:

请帮助我更清楚地显示它。 还告诉我如何显示块中出现的值,比如结构,因为它用日语写在数据库中,使用的排序规则是 utf8。

【问题讨论】:

    标签: python pandas matplotlib seaborn bar-chart


    【解决方案1】:

    当要可视化的对象很多时,有必要根据目的缩小目标范围。以下是来自kaggle 的样本数据示例,然后缩小到 2014 年、2015 年和 2016 年,类别数据有限。

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set_theme(style="whitegrid")
    
    df = pd.read_csv('./Data/vgsales.csv', sep=',')
    df = df[(df['Platform'] == 'NES') | (df['Platform'] == 'PS4') | (df['Platform'] == 'X360')]
    df = df[(df['Year'] == 2014.0) | (df['Year'] == 2015.0) | (df['Year'] == 2016.0)]
    
    df.info()
    <class 'pandas.core.frame.DataFrame'>
    Int64Index: 432 entries, 33 to 16570
    Data columns (total 11 columns):
     #   Column        Non-Null Count  Dtype  
    ---  ------        --------------  -----  
     0   Rank          432 non-null    int64  
     1   Name          432 non-null    object 
     2   Platform      432 non-null    object 
     3   Year          432 non-null    float64
     4   Genre         432 non-null    object 
     5   Publisher     431 non-null    object 
     6   NA_Sales      432 non-null    float64
     7   EU_Sales      432 non-null    float64
     8   JP_Sales      432 non-null    float64
     9   Other_Sales   432 non-null    float64
     10  Global_Sales  432 non-null    float64
    dtypes: float64(6), int64(1), object(4)
    memory usage: 40.5+ KB
    df.head(10)
            Rank    Name    Platform    Year    Genre   Publisher   NA_Sales    EU_Sales    JP_Sales    Other_Sales Global_Sales
    33  34  Call of Duty: Black Ops 3   PS4 2015.0  Shooter Activision  5.77    5.81    0.35    2.31    14.24
    44  45  Grand Theft Auto V  PS4 2014.0  Action  Take-Two Interactive    3.80    5.81    0.36    2.02    11.98
    77  78  FIFA 16 PS4 2015.0  Sports  Electronic Arts 1.11    6.06    0.06    1.26    8.49
    92  93  Star Wars Battlefront (2015)    PS4 2015.0  Shooter Electronic Arts 2.93    3.29    0.22    1.23    7.67
    93  94  Call of Duty: Advanced Warfare  PS4 2014.0  Shooter Activision  2.80    3.30    0.14    1.37    7.60
    109 110 Fallout 4   PS4 2015.0  Role-Playing    Bethesda Softworks  2.47    3.15    0.24    1.10    6.96
    124 125 FIFA 15 PS4 2014.0  Sports  Electronic Arts 0.79    4.29    0.05    1.47    6.59
    154 155 Destiny PS4 2014.0  Shooter Activision  2.49    2.05    0.16    0.96    5.65
    221 222 FIFA 17 PS4 2016.0  Sports  Electronic Arts 0.28    3.75    0.06    0.69    4.77
    236 237 The Last of Us  PS4 2014.0  Action  Sony Computer Entertainment 1.78    1.87    0.07    0.82    4.55
    

    除上述条件外,细化销售数量

    fig, ax = plt.subplots(figsize=(20, 9))
    g = sns.barplot(data=df[df['Global_Sales'] >= 1.0], x='Name', y='Global_Sales', palette='tab20', ax=ax)
    g.set_xticklabels(g.get_xticklabels(), rotation=90)
    
    plt.show()
    

    按销售年份绘制多年图表

    g = sns.catplot(data=df[df['Global_Sales'] >= 1.0], kind='bar', x='Name', y='Global_Sales', row='Year', palette='tab20', aspect=3, height=3)
    g.set_xticklabels(rotation=90)
    

    按类型分类(因为它是游戏)

    grid = sns.FacetGrid(data=df[df['Global_Sales'] >= 1.0], col="Genre", hue="Name", palette="tab20", col_wrap=3)
    grid.map(plt.bar, 'Year', 'Global_Sales')
    grid.set(xticks=[2014.0,2015.0,2016.0], yticks=[0,20], ylim=(0, 21))
    grid.fig.set_figheight(10)
    grid.fig.set_figwidth(20)
    grid.add_legend()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      相关资源
      最近更新 更多