条形图系列
在条形图的柱形变为横向,条形图更加强调项目之间的大小对比;尤其在项目名称较长以及数量较多时,采用条形图可视化数据会更加美观、清晰;
plotnine包绘制条形图
使用plotnine包绘制条形图,Y轴变量和图例变量默认按字母顺序绘制;在plotnine绘制柱形图时,添加coord_flip(),就可以将X-Y轴旋转,将柱形图转换成条形图;
plotnine绘制单数据系列条形图
import pandas as pd
import numpy as np
from plotnine import *
import matplotlib.pyplot as plt
df=pd.read_csv('d:\python\out\StackedbarD.csv')
df=df.sort_values(by='Pensions', ascending=True)
df['Country']=df['Country'].astype("category", df['Country'])
base_plot=(ggplot(df,aes('Country','Pensions'))+
geom_bar(stat="identity", color="black", width=0.6,fill="#FC4E07",size=0.25) +#"#00AFBB"
coord_flip()+
theme(
axis_title=element_text(size=15,face="plain",color="black"),
axis_text = element_text(size=12,face="plain",color="black"),
legend_title=element_text(size=13,face="plain",color="black"),
legend_position = "right",
aspect_ratio =1.15,
figure_size = (6.5, 6.5),
dpi = 50
))
print(base_plot)
plotnine绘制多数据系列条形图
import pandas as pd
import numpy as np
from plotnine import *
import matplotlib.pyplot as plt
df=pd.read_csv('d:\python\out\StackedbarD.csv')
df=df.iloc[:,[0,2,1]]
df=df.sort_values(by='Pensions', ascending=True)
mydata=pd.melt(df,id_vars='Country')
mydata['Country']=mydata['Country'].astype("category",df['Country'])
base_plot=(ggplot(mydata,aes('Country','value',fill='variable'))+
geom_bar(stat="identity", color="black", position=position_dodge(),width=0.7,size=0.25)+
scale_fill_manual(values=("#00AFBB", "#FC4E07", "#E7B800"))+
coord_flip()+
theme(
axis_title=element_text(size=15,face="plain",color="black"),
axis_text = element_text(size=12,face="plain",color="black"),
legend_title=element_text(size=14,face="plain",color="black"),
legend_background =element_blank(),
legend_position = (0.8,0.2),
aspect_ratio =1.15,
figure_size = (6.5, 6.5),
dpi = 50
))
print(base_plot)
plotnine绘制堆积条形图
import pandas as pd
import numpy as np
from plotnine import *
import matplotlib.pyplot as plt
df=pd.read_csv('d:\python\out\StackedbarD.csv')
Sum_df=df.iloc[:,1:].apply(lambda x: x.sum(), axis=0).sort_values(ascending=True)
meanRow_df=df.iloc[:,1:].apply(lambda x: x.mean(), axis=1)
Sing_df=df['Country'][meanRow_df.sort_values(ascending=True).index]
mydata=pd.melt(df,id_vars='Country')
mydata['variable']=mydata['variable'].astype("category",Sum_df.index)
mydata['Country']=mydata['Country'].astype("category",Sing_df)
base_plot=(ggplot(mydata,aes('Country','value',fill='variable'))+
geom_bar(stat="identity", color="black", position='stack',width=0.65,size=0.25)+
scale_fill_brewer(palette="YlOrRd")+
coord_flip()+
theme(
axis_title=element_text(size=18,face="plain",color="black"),
axis_text = element_text(size=16,face="plain",color="black"),
legend_title=element_text(size=18,face="plain",color="black"),
legend_text = element_text(size=16,face="plain",color="black"),
legend_background =element_blank(),
legend_position = 'right',
aspect_ratio =1.15,
figure_size = (6.5, 6.5),
dpi = 50
))
print(base_plot)
base_plot.save('d:\python\out\堆积条形图.pdf')
plotnine绘制百分比堆积条形图
import pandas as pd
import numpy as np
from plotnine import *
import matplotlib.pyplot as plt
df=pd.read_csv('d:\python\out\StackedbarD.csv')
SumCol_df=df.iloc[:,1:].apply(lambda x: x.sum(), axis=1)
df.iloc[:,1:]=df.iloc[:,1:].apply(lambda x: x/SumCol_df, axis=0)
meanRow_df=df.iloc[:,1:].apply(lambda x: x.mean(), axis=0).sort_values(ascending=True)
Per_df=df.loc[:,meanRow_df.idxmax()].sort_values(ascending=True)
Sing_df=df['Country'][Per_df.index]
mydata=pd.melt(df,id_vars='Country')
mydata['Country']=mydata['Country'].astype("category",Sing_df)
mydata['variable']=mydata['variable'].astype("category",meanRow_df.index)
base_plot=(ggplot(mydata,aes(x='Country',y='value',fill='variable'))
+geom_bar(stat="identity", color="black", position='fill',width=0.7,size=0.25)
+scale_fill_brewer(palette="GnBu")
+coord_flip()
+theme(
axis_title=element_text(size=18,face="plain",color="black"),
axis_text = element_text(size=16,face="plain",color="black"),
legend_title=element_text(size=18,face="plain",color="black"),
legend_text = element_text(size=16,face="plain",color="black"),
aspect_ratio =1.15,
figure_size = (6.5, 6.5),
dpi = 50
)
)
print(base_plot)
base_plot.save('d:\python\out\百分比堆积柱形图.pdf')
matplotlib包绘制条形图
使用matplotlib包绘制条形图,用plt.barh()函数替代plt.bar()就可以实现柱形图转换成条形图;X轴变成数值型坐标,Y轴变成类别型坐标;
matplotlib绘制堆积条形图
from matplotlib import cm,colors
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure, show, rc
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)
df=pd.read_csv('d:\python\out\StackedCD.csv')
df=df.set_index("Clarity")
Sum_df=df.apply(lambda y: y.sum(), axis=0).sort_values(ascending=False)
df=df.loc[:,Sum_df.index]
meanRow_df=df.apply(lambda y: y.mean(), axis=1)
Sing_df=meanRow_df.sort_values(ascending=False).index
n_row,n_col=df.shape
y_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_x=np.zeros(n_col)
fig=plt.figure(figsize=(5,5))
for i in range(n_row):
label=Sing_df[i]
plt.barh(y_value,df.loc[label,:],color=color[i],label=label,edgecolor='k', linewidth=0.25)
bottom_x=bottom_x+df.loc[label,:].values
plt.xticks(y_value,df.columns,size=10) #设置y轴刻度
plt.legend(loc=(1,0.3),ncol=1,frameon=False)
plt.grid(axis="x",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') # 设置左‘脊梁’为无色
matplotlib绘制百分比堆积条形图
from matplotlib import cm,colors
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure, show, rc
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)
df=pd.read_csv('d:\python\out\StackedCD.csv')
df=df.set_index("Clarity")
SumCol_df=df.apply(lambda y: y.sum(), axis=0)
df=df.apply(lambda y: y/SumCol_df, axis=1)
meanRow_df=df.apply(lambda y: y.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
y_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_x=np.zeros(n_col)
fig=plt.figure(figsize=(5,5))
for i in range(n_row):
label=Sing_df[i]
plt.barh(y_value,df.loc[label,:],color=color[i],label=label,edgecolor='k', linewidth=0.25)
bottom_x=bottom_x+df.loc[label,:].values
plt.yticks(y_value,df.columns,size=10) #设置y轴刻度
plt.gca().set_xticklabels(['{:.0f}%'.format(y*100) for y in plt.gca().get_xticks()])
plt.legend(loc=(1,0.3),ncol=1,frameon=False)
plt.grid(axis="x",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') # 设置左‘脊梁’为无色