当前位置:网站首页>matplotlib plt. Specific usage of text() - labeling points in a drawing

matplotlib plt. Specific usage of text() - labeling points in a drawing

2022-06-10 17:32:00 Guoqingru

matplotlib plt.text() Specific usage of —— When drawing a picture, label the points in the picture

 Insert picture description here

plt.text(x, y, s, fontsize, verticalalignment,horizontalalignment,rotation , **kwargs)

 Insert picture description here

import pandas as pd # Import data analysis module 
import matplotlib.pyplot as plt # Import drawing module class 
plt.rcParams['font.sans-serif']=['SimHei'] # The Chinese font in the figure is set to bold 
plt.rcParams['axes.unicode_minus']=False # Negative value display 
data=pd.read_excel(r"D:\ City rental map \ People flow and epidemic development \ Baidu migration .xlsx") # Reading data 
city_name=data['city'] # The city name 
people_flow=data['out_people']*100 # Outflow population , The unit has 100 people 
confirm=data['confirm(2.10)'] # Number of confirmed cases 

fig=plt.figure(figsize=(8,6)) # New canvas 
ax=plt.subplot(1,1,1) # Subgraph initialization 
ax.scatter(people_flow,confirm) # Draw a scatter plot    
ax.set_title(" Population inflow - Number of confirmed cases ")
ax.set_xlabel(" Population inflow ( Hundred people )")
ax.set_ylabel(" Number of confirmed cases ")
plt.show()

 Insert picture description here

ax.text(430, 337, " Beijing ", fontsize=12, color = "r", style = "italic", weight = "light", verticalalignment='center', horizontalalignment='right', rotation=90)

 Insert picture description here

for i in range(len(confirm)):
    ax.text(people_flow[i]*1.01, confirm[i]*1.01, city_name[i], fontsize=10, color = "r", style = "italic", weight = "light", verticalalignment='center', horizontalalignment='right',rotation=0) # Label the scatter 

 Insert picture description here

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 15 10:29:38 2020
project name:add_annotation
@author:  Handsome de Three uncles 
"""
import pandas as pd # Import data analysis module 
import matplotlib.pyplot as plt # Import drawing module class 
plt.rcParams['font.sans-serif']=['SimHei'] # The Chinese font in the figure is set to bold 
plt.rcParams['axes.unicode_minus']=False # Negative value display 
data=pd.read_excel(r"D:\ City rental map \ People flow and epidemic development \ Baidu migration .xlsx") # Reading data 
city_name=data['city'] # The city name 
people_flow=data['out_people']*100 # Outflow population , The unit has 100 people 
confirm=data['confirm(2.10)'] # Number of confirmed cases 

fig=plt.figure(figsize=(8,6)) # New canvas 
ax=plt.subplot(1,1,1) # Subgraph initialization 
ax.scatter(people_flow,confirm) # Draw a scatter plot    
ax.set_title(" Population inflow - Number of confirmed cases ")
ax.set_xlabel(" Population inflow ( Hundred people )")
ax.set_ylabel(" Number of confirmed cases ")
#ax.text(430, 337, " Beijing ", fontsize=12, color = "r", style = "italic", weight = "light", verticalalignment='center', horizontalalignment='right',rotation=90)
for i in range(len(confirm)):
    ax.text(people_flow[i]*1.01, confirm[i]*1.01, city_name[i], 
            fontsize=10, color = "r", style = "italic", weight = "light",
            verticalalignment='center', horizontalalignment='right',rotation=0) # Label the scatter 
plt.show()
原网站

版权声明
本文为[Guoqingru]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101637303917.html