【546】Python 绘制直方图
参考:5种方法教你用Python玩转histogram直方图
参考:Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度
设置百分比
- 创建函数
- 添加 weight
- 格式修改
1 2 3 4 5 6 7 | def to_percent(y,position): return str ( round ( 100 * y, 2 )) + "%" plt.hist(xx, bins, facecolor = 'blue' , edgecolor = 'black' , alpha = 0.7 , weights = [ 1. / len (xx)] * len (xx)) fomatter = FuncFormatter(to_percent) plt.gca().yaxis.set_major_formatter(fomatter) |
设置横坐标显示区间
- 通过设置 bins 来实现,每 10 个一个间隔
1 2 | xx = np.array(ratios_10m) bins = range ( 0 , 101 , 10 ) |
刻度显示
1 2 | plt.xlim( 0 , 100 ) plt.xticks( range ( 0 , 101 , 10 )) |
代码示例参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | from matplotlib.ticker import FuncFormatter import folium def to_percent(y,position): return str ( round ( 100 * y, 2 )) + "%" #for i in range(3): for i in range ( len (ratios_new)): print ( '*' * 100 ) print ( '*' * 100 ) print () print ( "AOI INDEX =" , i) print ( "包含点的数量:" , len (ratios_new[i][ 0 ])) print ( "10m 平均值:" , round ( sum (ratios_new[i][ 0 ]) / len (ratios_new[i][ 0 ]), 2 ), "%" ) print ( "20m 平均值:" , round ( sum (ratios_new[i][ 1 ]) / len (ratios_new[i][ 1 ]), 2 ), "%" ) df[df[ 'aoi_id' ] = = aoi_wj_new[i][ 0 ]][ 'addr' ].value_counts().head( 10 ) # 加载高德地图瓦片 pts = [[pt[ 1 ], pt[ 0 ]] for pt in aoi_wj_new[i][ 1 ].exterior.coords[:]] lats = [pt[ 0 ] for pt in pts] lngs = [pt[ 1 ] for pt in pts] m = folium. Map (location = [ sum (lats) / len (lats), sum (lngs) / len (lngs)], zoom_start = 15 , tiles = 'http://webst04.is.autonavi.com/appmaptile?style=7&x={x}&y={y}&z={z}' , attr = 'default' ) _ = folium.Polygon([[pt[ 1 ], pt[ 0 ]] for pt in aoi_wj_new[i][ 1 ].exterior.coords[:]], weight = 1.5 , fill_color = 'blue' ).add_to(m) m ratios_10m = ratios_new[i][ 0 ] ratios_20m = ratios_new[i][ 1 ] # 10m xx = np.array(ratios_10m) # 确定很坐标显示的范围 bins = range ( 0 , 101 , 10 ) _ = plt.hist(xx, bins, facecolor = 'blue' , edgecolor = 'black' , alpha = 0.7 , weights = [ 1. / len (xx)] * len (xx)) _ = plt.xlabel( "10m (%)" ) _ = plt.ylabel( "frequency" ) fomatter = FuncFormatter(to_percent) _ = plt.gca().yaxis.set_major_formatter(fomatter) _ = plt.xlim( 0 , 100 ) _ = plt.xticks( range ( 0 , 101 , 10 )) _ = plt.ylim( 0 , 1 ) _ = plt.title( "AOI index = " + str (i)) _ = plt.show() # 20m xx = np.array(ratios_20m) # 确定很坐标显示的范围 bins = range ( 0 , 101 , 10 ) _ = plt.hist(xx, bins, facecolor = 'blue' , edgecolor = 'black' , alpha = 0.7 , weights = [ 1. / len (xx)] * len (xx)) _ = plt.xlabel( "20m (%)" ) _ = plt.ylabel( "frequency" ) fomatter = FuncFormatter(to_percent) _ = plt.gca().yaxis.set_major_formatter(fomatter) _ = plt.xlim( 0 , 100 ) _ = plt.xticks( range ( 0 , 101 , 10 )) _ = plt.ylim( 0 , 1 ) _ = plt.title( "AOI index = " + str (i)) _ = plt.show() |
显示效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)