1. 单数据条形图
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams["font.sans-serif"]='SimHei'
plt.rcParams['axes.unicode_minus']=False
plt.rc('axes',axisbelow=True)
y_data = range(10,60,10)
x_data = range(1,6,1)
fig=plt.figure(figsize=(8,6),dpi=100)
ax = fig.add_subplot(1,1,1)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
ax.spines[['top','left','bottom','right']].set_linewidth(1.5)
ax.spines[['top','right']].set_color('none')
bars = ax.bar(x_data,y_data,width=0.6,align="center")
ax.tick_params(axis="x", direction='out', which='major',labelsize=16, length=5, width=1.5,)
ax.tick_params(axis="y", direction='in',which="major", labelsize=16, length=8, width=2, pad=5)
for bar in bars:
height = bar.get_height()
ax.annotate(f'{height+100:.1f}', xy=(bar.get_x() + bar.get_width() / 2, height),
fontsize=14, color='black',
xytext=(0, 4), textcoords='offset points', ha='center', va='bottom')
ax.set_ylim(0,55)
ax.set_xlabel('种类',fontsize=18,labelpad=6)
ax.set_ylabel('数量',fontsize=18,labelpad=6)
plt.show()
3、多类数据条形图
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
customers = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO']
customers_index = np.arange(len(customers))
sale_amounts = [127, 90, 201, 111, 232]
sale_amounts2 = [47, 30, 91, 301, 132]
sale_amounts3 = [87, 120, 41, 31, 332]
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(1,1,1)
width = 0.3
rects1 = ax1.bar(customers_index - width, sale_amounts, width=width ,align='center', color='#F8766D',label='1号商品')
rects2 = ax1.bar(customers_index , sale_amounts2, width=width ,align='center', color='#B79F00',label='2号商品')
rects3 = ax1.bar(customers_index + width, sale_amounts3, width=width ,align='center', color='#00BA38',label='3号商品')
ax1.bar_label(rects1,padding=3,**{'fontsize': 14})
ax1.bar_label(rects2,padding=3)
ax1.bar_label(rects3,padding=3)
ax1.xaxis.set_ticks_position('bottom')
ax1.yaxis.set_ticks_position('left')
ax1.set_xticks(customers_index)
ax1.set_xticklabels(customers)
ax1.xaxis.set_tick_params(labelrotation = 45, labelsize = 12)
ax1.set_xlabel('Customer Name', fontsize = 14)
ax1.yaxis.set_tick_params(which = "both", labelsize = 10)
ax1.set_ylabel('Sale Amount')
ax1.legend(title = "类别",
fontsize = 16,
title_fontsize = 15,
bbox_to_anchor = (1.01, 0.7))
plt.savefig('bar_plot.png', dpi=400, bbox_inches='tight')
plt.show()
3. 普通堆叠柱状图
df=pd.read_csv('StackedColumn_Data.csv')
df=df.set_index("Clarity")
Sum_df=df.apply(lambda x: x.sum(), axis=0).sort_values(ascending=False)
df=df.loc[:,Sum_df.index]
meanRow_df=df.apply(lambda x: x.mean(), axis=1)
Sing_df=meanRow_df.sort_values(ascending=False).index
n_row,n_col=df.shape
x_value=np.arange(n_col)
cmap=cm.get_cmap('YlOrRd_r',n_row)
color=[colors.rgb2hex(cmap(i)[:3]) for i in range(cmap.N) ]
bottom_y=np.zeros(n_col)
fig=plt.figure(figsize=(5,5))
for i in range(n_row):
label=Sing_df[i]
plt.bar(x_value,df.loc[label,:],bottom=bottom_y,width=0.5,color=color[i],label=label,edgecolor='k', linewidth=0.25)
bottom_y=bottom_y+df.loc[label,:].values
plt.xticks(x_value,df.columns,size=10)
plt.legend(loc=(1,0.3),ncol=1,frameon=False)
plt.grid(axis="y",c=(166/256,166/256,166/256))
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_color('none')
4. 百分比堆叠柱状图
df=pd.read_csv('StackedColumn_Data.csv')
df=df.set_index("Clarity")
SumCol_df=df.apply(lambda x: x.sum(), axis=0)
df=df.apply(lambda x: x/SumCol_df, axis=1)
meanRow_df=df.apply(lambda x: x.mean(), axis=1)
Per_df=df.loc[meanRow_df.idxmax(),:].sort_values(ascending=False)
Sing_df=meanRow_df.sort_values(ascending=False).index
df=df.loc[:,Per_df.index]
n_row,n_col=df.shape
x_value=np.arange(n_col)
cmap=cm.get_cmap('YlOrRd_r',n_row)
color=[colors.rgb2hex(cmap(i)[:3]) for i in range(cmap.N) ]
bottom_y=np.zeros(n_col)
fig=plt.figure(figsize=(5,5))
for i in range(n_row):
label=Sing_df[i]
plt.bar(x_value,df.loc[label,:],bottom=bottom_y,width=0.5,color=color[i],label=label,edgecolor='k', linewidth=0.25)
bottom_y=bottom_y+df.loc[label,:].values
plt.xticks(x_value,df.columns,size=10)
label_format = '{:.1f}%'
ylabels = ax.get_yticks().tolist()
ax.yaxis.set_major_locator(mticker.FixedLocator(ylabels))
ax.set_yticklabels([label_format.format(x*100) for x in ylabels])
plt.legend(loc=(1,0.3),ncol=1,frameon=False)
plt.grid(axis="y",c=(166/256,166/256,166/256))
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_color('none')
5. 柱形图加累计曲线(双Y轴坐标)
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.ticker import FuncFormatter
from matplotlib.pyplot import MultipleLocator
plt.style.use('ggplot')
mpl.rcParams['font.sans-serif'] = ['Times New Roman']
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
data = [0,1, 2, 3, 4,4,5, 5, 5, 5,6, 6, 6, 6, 6,7, 7,7,8, 9]
fig= plt.figure(figsize=(8, 4),dpi=100)
ax1 = fig.add_subplot(111)
a1,a2,a3=ax1.hist(data,bins =10, alpha = 0.65,edgecolor='k')
indexs=[]
a2=a2.tolist()
for i,value in enumerate(a2):
if i<=len(a2)-2:
index=(a2[i]+a2[i+1])/2
indexs.append(index)
def to_percent(temp,position):
return '%1.0f'%(100*temp) + '%'
dis=a2[1]-a2[0]
print('dis',dis)
freq=[f*dis for f in a1]
acc_freq=[]
for i in range(0,len(freq)):
if i==0:
temp=freq[0]
else:
temp=sum(freq[:i+1])
acc_freq.append(temp/102)
print('acc_freq',acc_freq)
print(sum(data))
ax2=ax1.twinx()
ax2.plot(indexs,acc_freq,color='#80b1d2')
ax2.yaxis.set_major_formatter(FuncFormatter(to_percent))
ax1.set_xlabel('x',fontsize=8)
ax1.set_title("title",fontsize =8)
ax1.set_ylabel('频率/组距',fontsize=8)
ax2.set_ylabel("累计频率",fontsize=8)
plt.show()