## 導入包 import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns ## 參數設置 #-------------------------01------------------------- large = 22; med = 16; small = 12 params = {'axes.titlesize': large, 'legend.fontsize': med, 'figure.figsize': (16, 10), 'axes.labelsize': med, 'axes.titlesize': med, 'xtick.labelsize': med, 'ytick.labelsize': med, 'figure.titlesize': large} plt.rcParams.update(params) plt.style.use('seaborn-whitegrid') sns.set_style("white") # 顯示中文 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False ## box #-------------------------01------------------------- fig=plt.figure(figsize=(10,6), dpi= 80) ax1=fig.add_subplot(121) sns.boxplot(x=personal_df.minutes,ax=ax1) ax1.set_xlabel('我的客戶') ax1.set_xlim(0,500) ax1.set_title('空調開啓時長分佈', fontsize=16, loc='center') ax1.legend() #-------------------------02------------------------- fig=plt.figure(figsize=(12,6), dpi= 80) ax2=fig.add_subplot(121) sns.boxplot(x='company_type', y='minutes',data = company_df,ax=ax2) ax2.set_xlabel('公司客戶') ax2.set_title('空調開啓時長分佈', fontsize=16, loc='center') ax2.legend() ## bar #-------------------------01------------------------- plt.figure(figsize=(10,6),dpi = 72) ax = sns.barplot(x=p_vehacfanspeed_times_mean_df.index.values, y=p_vehacfanspeed_times_mean_df.times_minutes.values, palette="rocket") ax.axhline(0, color="k", clip_on=False) ax.set_ylabel('平均時長/分鐘') ax.set_xlabel('空調風速') # ax.set_axis_off() ax.set_title('我的用戶', fontsize=24, loc="center", pad=10) # Annotate Text for i, cty in enumerate(p_vehacfanspeed_times_mean_df.times_minutes.values): ax.text(i, cty+0.5, round(cty, 1),fontsize=12, horizontalalignment='center') #-------------------------02------------------------- otal_width, n = 0.8, 2 # 有多少個類型,只需更改n便可 width = total_width / n # the width of the bars # x = x - (total_width - width) / 2 fig, ax = plt.subplots(figsize=(10,8),dpi= 80) rects1 = ax.bar(company_out_freq_count_s.index.values+width, company_out_freq_count_s.values, width,label = '公司客戶', color='r') rects2 = ax.bar(personal_out_freq_count_s.index.values, personal_out_freq_count_s.values, width,label = '我的客戶', color='b') def autolabel(rects): # attach some text labels for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., 1.02*height, '%d' % int(height), ha='center', va='bottom') autolabel(rects1) autolabel(rects2) ax.set_ylabel('頻數') ax.set_title('我的/公司車輛一天出行次數分佈') plt.legend() ## distplot #-------------------------01------------------------- fig=plt.figure(figsize=(16,6), dpi= 80) ax1=fig.add_subplot(121) #1*2的圖形 在第一個位置 sns.distplot(company_out_df['delta_time_h'], color="dodgerblue", label="company_out", hist_kws={'alpha':.7}, kde_kws={'linewidth':3},ax=ax1) sns.distplot(personal_out_df['delta_time_h'], color="orange", label="personal_out", hist_kws={'alpha':.7}, kde_kws={'linewidth':3},ax=ax1) ax1.set_ylim(0,1) # Decoration ax1.set_xlabel("單次出行時長/h") ax1.set_title('單次出行時長密度分佈', fontsize=22) ax1.legend() ## pie #-------------------------01------------------------- fig = plt.figure(figsize=(12, 8), dpi= 80) def func(pct, allvals): absolute = int(pct/100.*np.sum(allvals)) return "{:.1f}% ({:d} )".format(pct, absolute) ax1=fig.add_subplot(221) personal_charge_data = [personal_fcharge_car_nums, personal_scharge_car_nums] categories = pd.Series(data = ['快充', '慢充']) explode = [0,0.1] wedges, texts, autotexts = ax1.pie(personal_charge_data, autopct=lambda pct: func(pct, personal_charge_data), textprops=dict(color="w"), colors=plt.cm.Dark2.colors, startangle=140, explode=explode) # ax1.legend(wedges, categories, title="充電類型", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1)) ax1.set_title('我的充電類型佔比')
幫助文檔:
Matplotlib tutorial for beginner: https://github.com/rougier/matplotlib-tutorial
圖之典:http://tuzhidian.com/
例子:
effective data visualization
Matplotlib 中文文檔
notebook_matplotlib_visualizations.ipynb
top-50-matplotlib-visualizations-the-master-plots-python
art_of_data_visualization: text pre-processinghtml