The last original tweet used R-ggplot2 The realization of the economist's classic chart imitation R-ggplot2 The classic economist's chart imitates , So what about this issue , We just use Python-seaborn To reproduce this classic economist chart . The main knowledge points involved are as follows :
First , Let's preview the data ( part ):
among Region_new Columns are new columns that are changed according to relevant requirements , Mapping is also based on secondary data .
Use seaborn By drawing the fitting line, you can avoid making your own wheels , Now let's go straight to the basics ( Without any embellishment ), The code is as follows :
fig,ax = plt.subplots(figsize=(8,4.5),dpi=200,facecolor='white',edgecolor='white') ax.set_facecolor("white") fit_line = sns.regplot(data=test_data,x="CPI",y="HDI",logx=True,ax=ax) ax.text(.85,-.07,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8,color='black')
The visualization is as follows :
The main parameters needed here are as follows :
At present, only these ( Because drawing needs ), For more details, please refer to the corresponding official website :seaborn.regplot
Let's just put in the visual code that we're drawing , And then explain it separately , The code is as follows :
fig,ax = plt.subplots(figsize=(8,4.5),dpi=200,facecolor='white',edgecolor='white') ax.set_facecolor("white") color = [region_color[i] for i in test_data['Region_new']] fit_line = sns.regplot(data=test_data,x="CPI",y="HDI",logx=True,ci=False, line_kws={"color":"red", "label":r"$R^2$=56%", "lw":1.5}, scatter_kws={"s":50,"fc":"white", "ec":color, "lw":1.5, "alpha":1}, ax=ax) texts = [] for i, j ,t in zip(data_text["CPI"],data_text["HDI"],data_text["Country"]): texts.append(ax.annotate(t,xy=(i, j),xytext=(i-.8,j), arrowprops=dict(arrowstyle="-", color="black",lw=.5), color='black',size=9)) adjust_text(texts,only_move={'text': 'xy','objects':'x','point':'y'}) #adjust_text(texts,only_move={'text': 'xy'}) ax.set_xlabel("Corruption Perceptions Index, 2011 (10=least corrupt)",fontstyle="italic", fontsize=8) ax.set_ylabel("Human Development Index, 2011 (1=best)",fontstyle="italic",fontsize=8) ax.set_xlim((.5, 10.2)) ax.set_ylim((.2, 1)) ax.set_xticks(np.arange(1, 10.3, step=1)) ax.set_yticks(np.arange(0.2, 1.05, step=0.1)) # Grid settings ax.grid(which='major',axis='y',ls='-',c='gray',) ax.set_axisbelow(True) # Axis ridge setting for spine in ['top','left','right']: ax.spines[spine].set_visible(None) # Remove the axial ridge ax.spines['bottom'].set_color('k') # Set up bottom Color # Scale setting , Display only bottom The scale of , And the direction is outward , Long 、 Width is also set ax.tick_params(bottom=True,direction='in',labelsize=12,width=1,length=3, left=False) # Add legend ax.scatter([], [], ec='#01344A', fc="white",label='OECD', lw=1.5) ax.scatter([], [], ec='#228DBD', fc="white",label='Americas', lw=1.5) ax.scatter([], [], ec='#6DBBD8', fc="white",label='Asia & \nOceania', lw=1.5) ax.scatter([], [], ec='#1B6E64', fc="white",label='Central & \nEastern Europe', lw=1.5) ax.scatter([], [], ec='#D24131', fc="white",label='Middle East & \nnorth Africa', lw=1.5) ax.scatter([], [], ec='#621107', fc="white",label='Sub-Saharan \nAfrica', lw=1.5) ax.legend(loc="upper center",frameon=False,ncol=7,fontsize=6.5,bbox_to_anchor=(0.5, 1.1)) ax.text(.5,1.19,"Corruption and human development",transform = ax.transAxes,ha='center', va='center',fontweight="bold",fontsize=16) ax.text(.5,1.12, "Base Charts:Scatter Exercise in Python", transform = ax.transAxes,ha='center', va='center',fontsize = 12,color='black') ax.text(.9,.05,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8,color='black')
「 Knowledge point 」
color = ('#01344A','#228DBD','#6DBBD8','#1B6E64','#D24131','#621107') region =("OECD","Americas","Asia & \nOceania","Central & \nEastern Europe", "Middle East & \nnorth Africa", "Sub-Saharan \nAfrica") region_color = dict(zip(region,color)) color = [region_color[i] for i in test_data['Region_new']] # stay regplot() Call the following scatter_kws={"s":50,"fc":"white", "ec":color, "lw":1.5, "alpha":1}
texts = [] for i, j ,t in zip(data_text["CPI"],data_text["HDI"],data_text["Country"]): texts.append(ax.annotate(t,xy=(i, j),xytext=(i-.8,j), arrowprops=dict(arrowstyle="-", color="black",lw=.5), color='black',size=9)) adjust_text(texts,only_move={'text': 'xy','objects':'x','point':'y'})
# Add legend ax.scatter([], [], ec='#01344A', fc="white",label='OECD', lw=1.5) ax.scatter([], [], ec='#228DBD', fc="white",label='Americas', lw=1.5) ax.scatter([], [], ec='#6DBBD8', fc="white",label='Asia & \nOceania', lw=1.5) ax.scatter([], [], ec='#1B6E64', fc="white",label='Central & \nEastern Europe', lw=1.5) ax.scatter([], [], ec='#D24131', fc="white",label='Middle East & \nnorth Africa', lw=1.5) ax.scatter([], [], ec='#621107', fc="white",label='Sub-Saharan \nAfrica', lw=1.5) ax.legend(loc="upper center",frameon=False,ncol=7,fontsize=6.5,bbox_to_anchor=(0.5, 1.1))
The final visualization is as follows :
In this issue, we've launched Python-seaborn The classic visualization of tweets , Although there are still problems in the final result ( Of course , You can customize the specific location to solve ), But its main purpose is to let you learn drawing skills , Especially the drawing of fitting curve ( If you have wheels, you can use them directly , Don't think about reinventing yourself ).
This article is from WeChat official account. - DataCharm(shujumeili) , author : Ning Haitao
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .
Original publication time : 2020-11-22
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .