Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Hong Kong Map, House Price Visualization and Bubble Chart by Python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how Python realizes Hong Kong map, house price visualization and bubble chart". The explanation in the article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "how to achieve Hong Kong map, house price visualization and bubble chart by Python".

Preface

Today's tweet tutorials use geopandas to draw spatial charts (geopandas space drawing is very convenient, saving a lot of data processing, and it is also a perfect connection to matplotlib, friends who learn python space drawing can take a look at it), specifically for the drawing of space bubbles, the main contents are as follows:

Geopandas geojson data format is read and displayed visually

Add scatter size legend layers separately

AdjustText Library solves the problem of text overlap

Geopandas geojson data operation

Here we choose the geojson file data of Hong Kong Map, which can be downloaded from the DAtAV Map Selector. The downloaded file is named Hong Kong .json, and the visualization is as follows:

Data reading

You can easily read the data using the read_file () method of geopandas, as follows:

Hk_file = r "F:\ DataCharm\ commercial art chart imitation\ Hong Kong Map Visualization\ Hong Kong Special Administrative region .json" hk = geopandas.read_file (hk_file)

For more information on how to read data from geopandas, please refer to geopandas's official website for more information.

Data visualization display

After reading the data, we can directly use the plot () method of geopandas to draw, with the following code (with a simple color setting):

Fig, ax= plt.subplots (figsize= (10 off' 8), dpi=200) hk_map = hk.geometry.plot (ax=ax,fc= "# CCEBEB", ec= "# 009999", lw=1) ax.text (.91 nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8) ax.axis ('off') # remove axes plt.savefig (' hk_charts_pir.png',width=8,height=8, dpi=900,bbox_inches='tight') Facecolor='white')

The results are as follows:

Add area name text: name is listed as the corresponding area name in the read data result, and the longitude and latitude information of its representative points is calculated using the hk.geometry.representative_point () method to draw the text location. The results are as follows:

Then add the text by using the ax.text () method, as follows:

For loc, label in zip (hk.geometry.representative_point (), hk.name): ax.text (loc.x,loc.y,label,size=13,color= "# 0DCFE3")

The results are as follows:

Add bubble scatter data

Since the longitude and latitude of the data are parsed directly based on Amap, there are errors in the longitude and latitude information of some of the data. We use pandas for simple data filtering, which is no longer shown. A series of tutorial tweets will be launched later, and the data preview is as follows:

Here, we mainly use the data in the red box to draw, that is, we can use the scatter () method and set the scatter size reasonably. The code is as follows:

For xpeny quotation price in zip (scatter_se.lon,scatter_se.lat,scatter_se ['practical unit price']): hk_map.scatter (xmeme ypencil slotpriceAccorder 500 coloured colors, colors, books, books.

After some customized settings, the effects are as follows:

Bubble legend addition

Here, we do not generate the legend directly based on the data, but draw other layers separately to generate the legend. The advantage of this is that we can customize the color and size of the legend more freely. The code involved is as follows:

# A separate legend is added here to add ax.scatter ([], cantilever FFEB3Bs, swords 6000Comp500, label='6000-10000Universe, edgecolor='black',lw=.5) ax.scatter ([], [], cymbals FFEB3Bs, swords 10000Rand500, label='10000-3000mm, edgecolor='black',lw=.5) ax.scatter ([], [], cantilevered frames FFEB3Bs, scars 300000company500, label='30000-50000' Edgecolor='black',lw=.5) ax.scatter ([], cantilevered FFEB3Bs, slots 5000amp 500, label='50000-900000pm, edgecolor='black',lw=.5) # Legend customization settings legend = ax.legend (frameon=False,ncol=4,loc='lower right',title=' practical unit price', bbox_to_anchor= (1,-.06) Fontsize=9) legend.get_title () .set_color ('# ffffff') for text in legend.get_texts (): text.set_color ("# ffffff")

Note the second half of the code, which is a customized setting for matplotlib legend settings, as well as for other legends. The complete drawing code is as follows:

Fig, ax= plt.subplots (figsize= (10je 8), dpi=200,facecolor='#323332',edgecolor='#323332') ax.set_facecolor ('# 323332') hk_map = hk.geometry.plot (ax=ax,fc= "# 292200", ec= "gray", lw=1,alpha=.8) # adding text using default text causes text overlap for loc, label in zip (hk.geometry.representative_point (), hk.name): ax.text (loc.x,loc.y,label,size=11) Color= "# 0DCFE3") for xPowerypenny price in zip (scatter_se.lon,scatter_se.lat,scatter_se ['practical unit price']): hk_map.scatter (xrecedicepha.5) ax.axis ('off') # remove axes # here is a separate legend to add ax.scatter ([], [], cantilevered colors FFEB3Balls, slots 6000 500 cycles) Label='6000-10000, edgecolor='black',lw=.5) ax.scatter ([], [], cantilever FFEB3B, edgecolor='black',lw=.5) ax.scatter ([], [], cantilever FFEB3Bs, edgecolor='black',lw=.5) ax.scatter ([], [], cymbals FFEB3Bs, label='30000-50000cycles, edgecolor='black',lw=.5) ax.scatter ([], [], cymbals FFEB3Bs, scars 5000000laps Label='50000-9000000, edgecolor='black',lw=.5) # Legend customization setting legend = ax.legend (frameon=False,ncol=4,loc='lower right',title=' practical unit price', bbox_to_anchor= (1,-.06) Fontsize=9) legend.get_title (). Set_color ('# ffffff') for text in legend.get_texts (): text.set_color ("# ffffff") # add the necessary text: here title also uses the same method: ax.text (.5Power1.05, "Distribution Map of second-hand houses for sale in Hong Kong", transform = ax.transAxes,color= "white", weight='bold',size=20, ha='center', va='center') ax.text (.5) .985 nVisualization by DataCharm',transform 'data source: vegetable J learns Python',transform = ax.transAxes, ha='center', va='center',fontsize = 10 hk_charts.png',width=8,height=8 colorants white. ax.text (.91 maijin.07 nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8 miner colorants whiteite) plt.savefig (' hk_charts.png',width=8,height=8, dpi=900,bbox_inches='tight') Facecolor='#323332') # ax.set_axisbelow (True) plt.show ()

Visualization:

AdjustText Library solves the problem of text overlap

You may find that the text in the result picture is more concentrated, which may cause inconvenience to reading. We only use the adjustText package to solve the problem here. Here is the code to add the text. Other steps are the same:

From adjustText import adjust_text# uses adjustText to correct text reduplication new_texts = [ax.text (loc.x,loc.y,label,size=13,color= "# 0DCFE3") for loc, label in\ zip (hk.geometry.representative_point (), hk.name)] adjust_text (new_texts, only_move= {'text':' xy'},)

The visualization results are as follows:

Thank you for your reading. the above is the content of "how Python realizes Hong Kong map, house price visualization and drawing bubble chart". After the study of this article, I believe you have a deeper understanding of how Python realizes Hong Kong map, house price visualization and bubble chart. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report