python--柱狀圖散點圖

python–柱狀圖散點圖

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib as mpl

# 準備數據
name_new_list = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
num_sale_list = [369,324,400,388,260,81,27,22,0,0,0,113]
x = [c for c in range(len(name_new_list))]
sum_ = sum(num_sale_list)
num_list_new = [x/sum_*100 for x in num_sale_list]
# 調整圖片大小
plt.rcParams['figure.figsize'] = (12.0, 12.0)
# 調整分辨率
plt.figure(dpi=800)
# 畫直方圖
ax1 = sns.barplot(x=name_new_list, y=num_sale_list,color='lightskyblue',label=u'銷量')
# 圖例
plt.legend(loc=2)
# 畫折線圖
ax2 = ax1.twinx()
ax2.plot(x, num_list_new, marker='o', mec='r', mfc='w',color='r',label=u'銷量佔比')
# 添加數據標籤
for xy in zip(x,num_list_new):
    plt.annotate(str('%.0f'%xy[1])+'%', xy=xy, xytext=(0,0), textcoords = 'offset points',color='r')
# 調整座標軸範圍
ax1.set_ylim(0,max(num_sale_list)*1.2)
ax2.set_ylim(0, max(num_list_new)*1.2)
ax1.set_ylabel(u'銷量')
ax2.set_ylabel(u'銷量佔比')
# 調整x軸標籤大小
ax1.tick_params(axis='x', labelsize=14)
# 圖例
plt.legend(loc=1)
# 標題
plt.title ( '月銷量分佈情況')

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章