import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False
import warnings
warnings.filterwarnings("ignore")
import os
os.chdir(r'C:\Users\\ABC\Desktop')
data = pd.read_csv('order.csv',encoding='gbk')
plt.figure(figsize=(4,4),dpi=150)
dataname = data['产品类别']
freq = dataname.value_counts()
colors = ['#99CCFF','#CCFF66','#FFCC99']
plt.pie(freq, labels = freq.index, explode = (0.05, 0, 0), autopct = '%.1f%%', textprops={'fontsize': 12}, colors = colors, startangle = 90, counterclock = False)
plt.axis('square')
plt.legend(loc='upper right', bbox_to_anchor=(1.2, 0.2),prop={'size': 10})
plt.show()
plt.figure(figsize=(6,3),dpi=150)
color_palette = sns.color_palette()
plt.subplot(121)
sns.countplot('区域',data = data, palette=color_palette)
plt.xlabel('区域',fontsize=10)
plt.xticks(fontsize=10)
plt.tight_layout()
plt.subplot(122)
sns.countplot('产品类别',data = data)
plt.xlabel('产品类别',fontsize=10)
plt.xticks(fontsize=10)
plt.tight_layout()
color = sns.color_palette()
plt.figure(figsize=(15,7))
sns.countplot(x = '产品名称',data = data, order = data['产品名称'].value_counts().index)
plt.xlabel('产品名称',fontsize=15)
plt.ylabel('count',fontsize=15)
plt.xticks(fontsize=15,rotation=45)
plt.yticks(fontsize=15)
plt.show()
plt.figure(1 , figsize = (10 , 14))
sns.barplot(data["产品型号名称"].value_counts(dropna=False),
data["产品型号名称"].value_counts(dropna=False).keys())
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.xlabel('产品型号名称',fontsize=16)
plt.show()
plt.figure(figsize=(7,7))
df = data[['产品成本','利润','单价','销售金额']]
corr = df.corr()
sns.heatmap(df.corr(), center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5},annot=True, fmt='.1f')
plt.tight_layout()
plt.figure(figsize=(13,5))
color = sns.color_palette()
plt.subplot(141)
sns.boxplot(data=data['产品成本'],color=color[1])
plt.xlabel('产品成本',fontsize=15)
plt.yticks(fontsize=15)
plt.tight_layout()
plt.subplot(142)
sns.boxplot(data=data['利润'],color=color[2])
plt.xlabel('利润',fontsize=15)
plt.yticks(fontsize=15)
plt.tight_layout()
plt.subplot(143)
sns.boxplot(data=data['单价'],color=color[3])
plt.xlabel('单价',fontsize=15)
plt.yticks(fontsize=15)
plt.tight_layout()
plt.subplot(144)
sns.boxplot(data=data['销售金额'],color=color[3])
plt.xlabel('销售金额',fontsize=15)
plt.yticks(fontsize=15)
plt.tight_layout()
plt.show()
plt.figure(figsize=(12,7))
sns.countplot(x = '区域',hue = '交易类型',data = data)
plt.xlabel('区域',fontsize=15)
plt.yticks(fontsize=15)
plt.show()
sns.set_style("whitegrid")
fig,axes=plt.subplots(nrows=2,ncols=2,figsize=(17,7))
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False
df = data[['销售大区','国家','区域','产品类别']]
for i, item in enumerate(df):
plt.subplot(2,2,(i+1))
ax=sns.countplot(item,data = df,palette="Pastel1")
plt.xlabel(str(item),fontsize=14)
plt.ylabel('Count',fontsize=10)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
i=i+1
plt.tight_layout()
plt.show()
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False
df = pd.pivot_table(data, values=["产品成本",'利润','销售金额'], index=["国家"], aggfunc=np.sum)
labels = df.index.tolist()
y1 = [round(i/10000,1) for i in df['产品成本'].values.tolist()]
y2 = [round(i/10000,1) for i in df['利润'].values.tolist()]
y3 = [round(i/10000,1) for i in df['销售金额'].values.tolist()]
plt.rcParams['font.family'] = ['Times New Roman']
fig,ax = plt.subplots(1,1,figsize=(13,6))
width = 0.35
label_font = {
'weight':'bold',
'size':14,
'family':'simsun'
}
x = np.arange(len(labels))
total_width, n = 0.8, 3
width = total_width / n
x = x - (total_width - width) / 2
rects1 = ax.bar(x, y1, width, label='产品成本',ec='k',color='w',lw=.8,
hatch='xxx')
rects2 = ax.bar(x + width, y2, width, label='利润',ec='k',color='w',
lw=.8,hatch='//')
rects3 = ax.bar(x + width * 2, y3, width, label='销售金额',ec='k',color='w',
lw=.8,hatch='---')
ax.tick_params(which='major',direction='in',length=5,width=1.5,labelsize=11,bottom=False)
ax.tick_params(axis='x',labelsize=11,bottom=False,labelrotation=0)
ax.set_xticks(range(len(labels)))
ax.set_xlabel('国家',fontdict=label_font)
ax.set_ylim(ymin = 0,ymax = 1500)
ax.set_ylabel('单位:万元',fontdict=label_font)
ax.set_xticklabels(labels,fontdict=label_font)
ax.legend(prop =label_font)
linewidth = 2
for spine in ['top','bottom','left','right']:
ax.spines[spine].set_linewidth(linewidth)
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3),
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
fig.tight_layout()
plt.show()
plt.figure(1 , figsize = (10 , 7))
sns.distplot(data['单价'],bins=40)
plt.xlabel('单价',fontsize=15)
plt.ylabel('Density',fontsize=15)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.show()
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType
from pyecharts.faker import Faker
dataname = data['销售大区']
freq = dataname.value_counts()
c = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
.add(
"",
[list(z) for z in zip(freq.index.tolist(), freq.values.tolist())],
radius=["40%", "75%"],
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Pie-Radius"),
legend_opts=opts.LegendOpts(
orient="vertical",
pos_top="15%",
pos_left="2%"),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
c.render_notebook()
import pyecharts.options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType
dataname = data['国家']
freq = dataname.value_counts()
x_data = freq.index.tolist()
y_data = freq.values.tolist()
data_pair = [list(z) for z in zip(x_data, y_data)]
data_pair.sort(key=lambda x: x[1])
c=(
Pie(init_opts=opts.InitOpts(
width="900px",
height="600px",
theme=ThemeType.MACARONS))
.add(
series_name="访问来源",
data_pair=data_pair,
rosetype="radius",
radius="55%",
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=False, position="center"),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="Customized Pie",
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
),
legend_opts=opts.LegendOpts(is_show=True),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
label_opts=opts.LabelOpts(color="#3333FF"),
)
)
c.render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
dataname1 = data['区域']
freq1 = dataname1.value_counts()
dataname2 = data['产品类别']
freq2 = dataname2.value_counts()
c = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
.add(
"",
[list(z) for z in zip(freq1.index.tolist(), freq1.values.tolist())],
radius=["30%", "75%"],
center=["25%", "50%"],
rosetype="radius",
)
.add(
"",
[list(z) for z in zip(freq2.index.tolist(), freq2.values.tolist())],
radius=["30%", "75%"],
center=["75%", "50%"],
rosetype="area",
)
.set_global_opts(title_opts=opts.TitleOpts(title=""))
)
c.render_notebook()
|