MQTT 协议快速入门
2026/4/15 8:58:38
importmatplotlib.pyplotaspltimportpandasaspd以下数据如果有需要的同学可关注公众号HsuHeinrich,回复【数据可视化】自动获取~
url="https://raw.githubusercontent.com/holtzy/the-python-graph-gallery/master/static/data/data-CO2.csv"df=pd.read_csv(url)df.head()Name:国家地区
:x轴位置
:y轴位置
CO2 per Capita:人均二氧化碳排放量
Color:颜色
# 初始化fig,ax=plt.subplots(figsize=(6,6))# 绘制散点图ax.scatter(df[' '],# x位置df[' .1'],# y位置s=df['CO2 per Capita']*10,# 气泡大小)# 标题ax.set_title('The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions',weight='bold')ax.set_xlabel('Vulnerability')ax.set_ylabel('Readiness')plt.show()# 初始化fig,ax=plt.subplots(figsize=(6,6))# 绘制散点图ax.scatter(df[' '],# x位置df[' .1'],# y位置s=df['CO2 per Capita']*10,# 气泡大小c=df['Color'],# 颜色)# 标题ax.set_title('The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions',weight='bold')ax.set_xlabel('Vulnerability')ax.set_ylabel('Readiness',rotation=0,# 水平方向)# 移除边框和轴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(axis='both',which='both',length=0)plt.show()# 初始化fig,ax=plt.subplots(figsize=(6,6))# 绘制散点图ax.scatter(df[' '],# x位置df[' .1'],# y位置s=df['CO2 per Capita']*10,# 气泡大小c=df['Color'],# 颜色marker='s')# 标题ax.set_title('The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions',weight='bold')# 添加参考线ax.axvline(0.43,color='gray',linestyle='--',linewidth=0.7,alpha=0.4)ax.axhline(0.41,color='gray',linestyle='--',linewidth=0.7,alpha=0.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(axis='both',which='both',length=0)plt.show()defcircle_countries(country_names:list):''' 输出包含该国家地区的边框 '''# 将边缘颜色参数初始化为默认值:与字体颜色相同df['EdgeColor']=df['Color']# 更改指定国家的边缘颜色df.loc[df['Name'].isin(country_names),'EdgeColor']='black'returndf['EdgeColor']defadd_country_name(country_names:list):''' 在标记顶部添加国家地区名称 '''# 迭代每个国家地区forcountry_nameincountry_names:# 查找国家地区在轴上的位置x_axis=df.loc[df['Name']==country_name,' ']y_axis=df.loc[df['Name']==country_name,' .1']# 预防报警,采取警告中建议的方式处理x_axis_value=float(x_axis.iloc[0])y_axis_value=float(y_axis.iloc[0])# 将文本添加到正确的位置,稍微移动到顶部以方便阅读ax.text(x_axis_value,y_axis_value+0.025,# 位置country_name,# 标签size=6,# 文本大小ha='center',# 居中对齐)# 初始化fig,ax=plt.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 listedgecolors=circle_countries(country_to_circle)# 绘制散点图ax.scatter(df[' '],# x位置df[' .1'],# y位置s=df['CO2 per Capita']*10,# 气泡大小c=df['Color'],# 颜色edgecolor=edgecolors,linewidths=0.6,marker='s',zorder=2)# 添加国家地区名称add_country_name(country_to_circle)# 标题title='The countries with the highest vulnerability to climate change\nhave the lowest CO2 emissions'fig.text(0,0.97,# 相对位置title,fontsize=11,ha='left',family='dejavu sans',weight='bold')# 子标题subtitle='All countries sorted by their vulnerability and readiness to climate change. The size shows the CO2 emission\nper person in that country'fig.text(0,0.92,# 相对位置subtitle,fontsize=8,ha='left',family='dejavu sans',multialignment='left')# 添加参考线ax.axvline(0.43,color='gray',linestyle='--',linewidth=0.7,alpha=0.4)ax.axhline(0.41,color='gray',linestyle='--',linewidth=0.7,alpha=0.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(axis='both',which='both',length=0)# 添加文本注释标签fig.text(0.1,0.45,'High readiness',color='silver',size=8)fig.text(0.1,0.4,'Low readiness',color='silver',size=8)# 在上述注释标签周围添加箭头arrowprops=dict(arrowstyle="->",color='silver',lw=0.4)ax.annotate("",xy=(0.25,0.32),xytext=(0.25,0.37),arrowprops=arrowprops)ax.annotate("",xy=(0.25,0.5),xytext=(0.25,0.45),arrowprops=arrowprops)plt.show()参考:Bubble plot with specific annotations and customization
共勉~