建筑业招聘网站,网络推广文案前景,专业做网文的网站好,沭阳做网站利用气泡图探索二氧化碳排放与气候变化脆弱性的关系
import matplotlib.pyplot as plt
import pandas as pd数据探索 以下数据如果有需要的同学可关注公众号HsuHeinrich#xff0c;回复【数据可视化】自动获取#xff5e; url https://raw.githubusercontent.com/hol…利用气泡图探索二氧化碳排放与气候变化脆弱性的关系importmatplotlib.pyplotaspltimportpandasaspd数据探索以下数据如果有需要的同学可关注公众号HsuHeinrich回复【数据可视化】自动获取urlhttps://raw.githubusercontent.com/holtzy/the-python-graph-gallery/master/static/data/data-CO2.csvdfpd.read_csv(url)df.head()Name国家地区x轴位置y轴位置CO2 per Capita人均二氧化碳排放量Color颜色绘制基本气泡图# 初始化fig,axplt.subplots(figsize(6,6))# 绘制散点图ax.scatter(df[nbsp;],# x位置df[nbsp;.1],# y位置sdf[CO2 per Capita]*10,# 气泡大小)# 标题ax.set_title(The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions,weightbold)ax.set_xlabel(Vulnerability)ax.set_ylabel(Readiness)plt.show()自定义颜色和布局# 初始化fig,axplt.subplots(figsize(6,6))# 绘制散点图ax.scatter(df[nbsp;],# x位置df[nbsp;.1],# y位置sdf[CO2 per Capita]*10,# 气泡大小cdf[Color],# 颜色)# 标题ax.set_title(The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions,weightbold)ax.set_xlabel(Vulnerability)ax.set_ylabel(Readiness,rotation0,# 水平方向)# 移除边框和轴ax.spines[top].set_visible(False)ax.spines[right].set_visible(False)ax.spines[bottom].set_visible(False)ax.spines[left].set_visible(False)ax.set_xticklabels([])ax.set_yticklabels([])ax.tick_params(axisboth,whichboth,length0)plt.show()更新标记并添加参考线# 初始化fig,axplt.subplots(figsize(6,6))# 绘制散点图ax.scatter(df[nbsp;],# x位置df[nbsp;.1],# y位置sdf[CO2 per Capita]*10,# 气泡大小cdf[Color],# 颜色markers)# 标题ax.set_title(The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions,weightbold)# 添加参考线ax.axvline(0.43,colorgray,linestyle--,linewidth0.7,alpha0.4)ax.axhline(0.41,colorgray,linestyle--,linewidth0.7,alpha0.4)# 移除边框和轴ax.spines[top].set_visible(False)ax.spines[right].set_visible(False)ax.spines[bottom].set_visible(False)ax.spines[left].set_visible(False)ax.set_xticklabels([])ax.set_yticklabels([])ax.tick_params(axisboth,whichboth,length0)plt.show()突出注释部分国家地区defcircle_countries(country_names:list): 输出包含该国家地区的边框 # 将边缘颜色参数初始化为默认值与字体颜色相同df[EdgeColor]df[Color]# 更改指定国家的边缘颜色df.loc[df[Name].isin(country_names),EdgeColor]blackreturndf[EdgeColor]defadd_country_name(country_names:list): 在标记顶部添加国家地区名称 # 迭代每个国家地区forcountry_nameincountry_names:# 查找国家地区在轴上的位置x_axisdf.loc[df[Name]country_name,nbsp;]y_axisdf.loc[df[Name]country_name,nbsp;.1]# 预防报警采取警告中建议的方式处理x_axis_valuefloat(x_axis.iloc[0])y_axis_valuefloat(y_axis.iloc[0])# 将文本添加到正确的位置稍微移动到顶部以方便阅读ax.text(x_axis_value,y_axis_value0.025,# 位置country_name,# 标签size6,# 文本大小hacenter,# 居中对齐)# 初始化fig,axplt.subplots(figsize(6,6))# 需要突出的国家地区列表country_to_circle[Norway,Singapore,U.S.,Czech Republic,Qatar,Bahrain,Somalia,Sudan,India,Trinidad and Tobago,Chad]# Define the edgecolors according to the listedgecolorscircle_countries(country_to_circle)# 绘制散点图ax.scatter(df[nbsp;],# x位置df[nbsp;.1],# y位置sdf[CO2 per Capita]*10,# 气泡大小cdf[Color],# 颜色edgecoloredgecolors,linewidths0.6,markers,zorder2)# 添加国家地区名称add_country_name(country_to_circle)# 标题titleThe countries with the highest vulnerability to climate change\nhave the lowest CO2 emissionsfig.text(0,0.97,# 相对位置title,fontsize11,haleft,familydejavu sans,weightbold)# 子标题subtitleAll countries sorted by their vulnerability and readiness to climate change. The size shows the CO2 emission\nper person in that countryfig.text(0,0.92,# 相对位置subtitle,fontsize8,haleft,familydejavu sans,multialignmentleft)# 添加参考线ax.axvline(0.43,colorgray,linestyle--,linewidth0.7,alpha0.4)ax.axhline(0.41,colorgray,linestyle--,linewidth0.7,alpha0.4)# 移除边框和轴ax.spines[top].set_visible(False)ax.spines[right].set_visible(False)ax.spines[bottom].set_visible(False)ax.spines[left].set_visible(False)ax.set_xticklabels([])ax.set_yticklabels([])ax.tick_params(axisboth,whichboth,length0)# 添加文本注释标签fig.text(0.1,0.45,High readiness,colorsilver,size8)fig.text(0.1,0.4,Low readiness,colorsilver,size8)# 在上述注释标签周围添加箭头arrowpropsdict(arrowstyle-,colorsilver,lw0.4)ax.annotate(,xy(0.25,0.32),xytext(0.25,0.37),arrowpropsarrowprops)ax.annotate(,xy(0.25,0.5),xytext(0.25,0.45),arrowpropsarrowprops)plt.show()参考Bubble plot with specific annotations and customization共勉