设置条形图的宽度、大小和所有尺寸(seaborn)

2024-04-26 23:29:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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()

以下是执行此代码段时出现的条形图:

Barplot

请帮我把它展示得更清楚些。 另外,请告诉我如何显示块中出现的值,如用日语在数据库中写入的结构,所使用的排序规则为utf8


Tags: importbottledb产品asmysqlprodseaborn
1条回答
网友
1楼 · 发布于 2024-04-26 23:29:57

当需要可视化的对象很多时,有必要根据目的缩小目标范围。下面是一个示例,样本数据来自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()

enter image description here

按销售年度绘制多个年份的图表

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)

enter image description here

分类(因为它是一款游戏,所以按类型分类)

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()

enter image description here

相关问题 更多 >