Hover over an area a text should appear - python
I am currently viewing the neighborhoods. However, when I hover the mouse over it, I want a text to be displayed with the neighborhood and the average price. I have pasted my code and my card what I made. Below you can see how I would like it.
How can I display a text when I hover over it?
import folium
from folium.plugins import FastMarkerCluster
from branca.colormap import LinearColormap
map_ams_price = folium.Map(location=[52.3680, 4.9036], zoom_start=11, tiles="cartodbpositron")
map_ams_price.choropleth(
geo_data=r'C:\neighbourhoods.geojson',
data=df,
columns=['neighbourhood_cleansed', 'price'],
key_on='feature.properties.neighbourhood',
fill_color='BuPu',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Avg',
reset=True,
tooltip=folium.features.GeoJsonTooltip(fields=['neighbourhood_cleansed', 'price'],
labels=True,
sticky=False),
)
map_ams_price
Minium Example
# the price is the avg price
d = {'neighbourhood_cleansed': ['Oostelijk Havengebied - Indische Buurt', 'Centrum-Oost',
'Centrum-West', 'Zuid', 'De Baarsjes - Oud-West', 'Bos en Lommer',
'De Pijp - Rivierenbuurt', 'Oud-Oost', 'Noord-West', 'Westerpark',
'Slotervaart', 'Oud-Noord', 'Watergraafsmeer',
'IJburg - Zeeburgereiland', 'Noord-Oost', 'Buitenveldert - Zuidas',
'Geuzenveld - Slotermeer', 'De Aker - Nieuw Sloten', 'Osdorp',
'Bijlmer-Centrum', 'Gaasperdam - Driemond', 'Bijlmer-Oost'],
'price': [20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0]}
df = pd.DataFrame(data=d)
You can find the geojson here https://pastecode.io/s/a76fdvey
As I mentioned in the comments, in order to give tooltips in the choropleth map, they have to be in the geojson file, not in the dataframe value, in order to be displayed. So I used geopandas for the geojson file to be used, combined the data frames and added the price information to the geojson file. The column names in the original data frame have been modified to match the geojson file. It can also be used as a label by adding an alias name. The tooltip can be styled, so I added that as well.
import json
import requests
import geopandas as gpd
url = "http://data.insideairbnb.com/the-netherlands/north-holland/amsterdam/2021-09-07/visualisations/neighbourhoods.geojson"
gpd_geo = gpd.read_file(url)
gpd_geo = gpd_geo.merge(df, on='neighbourhood')
geo_json_data = gpd_geo.to_json()
import folium
from folium.plugins import FastMarkerCluster
from branca.colormap import LinearColormap
from folium.features import GeoJsonPopup, GeoJsonTooltip
map_ams_price = folium.Map(location=[52.3680, 4.9036], zoom_start=11, tiles="cartodbpositron")
choropleth = folium.Choropleth(
geo_data=geo_json_data,
data=df,
columns=['neighbourhood', 'price'],
key_on='feature.properties.neighbourhood',
fill_color='BuPu',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Avg',
reset=True,
highlight=True,
).add_to(map_ams_price)
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(fields=['neighbourhood', 'price'],
aliases=['neighbourhood:', 'average_price:'],
labels=True,
localize=True,
sticky=False,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",)
)
map_ams_price
Related
show cluster of point with folium
I have a dataframe with a list of coordinates. I want to group on the map near points and show a dot on the map with the number of points on it like this. I'm using geopandas and folium. Following this example this is my code from folium.plugins import MarkerCluster import geopandas as gdp from shapely.geometry import Point, LineString, Polygon, MultiPoint building_list = [(-1,-1),(10.6,-1),(10.6,4.4),(-1,4.4)] building = Polygon(building_list) gdf_building = gpd.GeoDataFrame({'building':['Building'], 'geometry':[building]}) locations = df[['x', 'y']] locationlist = locations.values.tolist() print(locationlist) map_building = gdf_building.explore(style_kwds={'color':'black','weight':3,'fillColor':'gray','fillOpacity':0.2}) marker_cluster = MarkerCluster().add_to(map_building) #add a marker to the cluster, not the map for location in locationlist: marker = folium.Marker( location=location, ) marker_cluster.add_child(marker) folium.LayerControl().add_to(map_building); map_building this is the result of the print of the location list, as you can see there are actual points. [[-10.975653734125247, 14.223493380357969], [-3.0008170610241343, 4.952113879109663], [1.2889747510247407, 2.204612243849062], [1.8083739029012205, 1.7796964504195971], [3.420669729369738, 0.7404826646612623], [4.754666507090696, -0.5524039121778554], [5.53171781359429, 1.310518688820813], [4.318223656736064, -0.8753972819321212], [1.389494271129264, 16.407899298913147], [2.82329764017852, 0.9072912833977023], [-4.497070654458373, 11.375240775117195], [4.388070857027747, 2.026226808689793], [5.020759388491676, 2.756120554023195], [6.001047947560095, 3.640750189810609], [4.748181698919979, 0.6264535181440452], [2.6528944739060116, 3.4802260546999184], [0.6549886054089478, 2.3877498852531], [0.9196638849958464, 2.150417536934342], [5.108903278970173, 1.7149961891938221], [4.023317826279737, -0.7706685254504961], [0.903562864987411, 2.0796324380334292], [1.9513434378658632, 1.3693238231850704], [5.644520571004791, 0.9779979070472828], [4.885749810717222, 1.8987377699798547], [0.8962029416395766, 2.1062557612048933], [4.8, 3.1279064752127708], [-0.04508236022751255, 3.0568571249844774], [4.800000000000001, 1.9140378892025933], [-1.989743839191096, 3.371668995303246], [1.1656083298774806, 1.807252953229595], [1.8713291261582097, 1.143179456971009], [4.874256276815991, 1.442130685409675], [1.4180968883141027, 1.6105182232356952], [3.190937527417693, 0.7681078921163724], [0.6152561767926022, 2.4352310754461803], [4.943357091884491, 1.3116890391870286], [4.669880127812657, -0.9679152111322677], [3.614139957564433, 4.902972429576663], [5.669098065784623, 3.913649249046372], [3.813973864130181, -0.7323384613836201], [-0.5199470910058632, 4.940176537119433], [0.6045522916902855, 2.4454362761206676], [3.997321854441272, -0.40367758748908944], [-0.7749524129532896, 3.7357518435721646], [4.398949303156029, 1.5109906728678215], [0.3021200538833131, 2.729947426419629], [5.644520571004791, 1.1699695341101362], [0.3021200538833131, 2.729947426419629], [4.038514768738497, -0.2998527712422736], [5.463665551081531, 4.137680366555347], [0.8962029416395773, 2.157030296469887], [3.4127967638900434, -0.15739477788275646], [5.3627337631140914, 0.47173132205910173], [-17.039850506402114, 17.631059209559115], [4.693589821903688, 2.645652867319201], [1.652915853616594, 1.4812029360906247], [0.7676072335427175, 2.7523632254899515], [5.108903278970173, 1.6583572860511704], [0.4132781963362031, 2.904134388098682], [3.259934813790278, -0.18044453177741993], [0.8945938791669944, 2.1032269377270914], [2.57881016023833, 3.112861191160806], [3.592593833132784, 1.4836895239891579], [3.317682346796072, -0.406574363983617], [5.23674698889199, -0.4896527267915487], [1.652915853616594, 1.3490348417130233], [4.085742861634555, 0.6349594490258132], [4.0072168062713, -1.0764854252471776], [3.9664315265442545, -0.39306568870652514], [0.6031981581824843, 2.377481733947807], [3.6039665183706195, -0.2228663982705159], [1.8714318174071345, 1.1838500546775474], [1.8733352672556218, 1.1469557225661375], [6.102634326393458, 10.469900068262838], [5.520832421942425, 2.46410701016014], [1.1747275757821485, 1.930301474093356], [4.67585787292738, 0.636918779066237], [4.491096721029827, 1.397978478475737], [1.4568609922766316, 1.6834859483416318], [0.6098506962014651, 2.302784409097839], [4.324311646727075, 2.936569893754537], [0.9333579831295232, 2.088974913476743], [4.518788267677348, 0.020784625382443522], [0.47410395349115, 4.18998422562209], [1.1656853374503982, 1.7606554558367773], [1.837995743774393, 4.916814195653471], [4.388070857027747, 2.3671414910993596], [4.174911828673442, -0.6971612141229651], [0.6031981581824846, 2.357970915575524], [1.2974476846606793, 3.9025406503211726], [-0.03437847512519587, 2.924380280250848], [4.174911828673442, -0.6971612141229651], [1.4936152461189462, 7.123830497692014], [0.6152561767926021, 2.2589820889454635], [-11.279626246016324, 13.226577973666835], [1.82048818578873, 4.061094403724194], [4.581484036209459, 5.890145802013782], [4.265190300374529, 2.452440089435306], [3.8845082501175248, -0.7686579417306034], [0.9637552343186307, 2.2334130180125245], [4.25063438297424, -0.843761982823303], [0.98431156175139, 4.194028317706987], [1.1761611467009938, 1.827117079015031], [5.449834811820658, 0.5594950288780689], [1.6724495566887114, 4.605197843492187], [3.5384417352877477, 0.07911340357174579], [4.348620907925173, -0.9172910153848934], [1.2159563418949306, 4.6362987571816525], [1.1668919640810418, 1.4915162635203172], [2.249924886746646, 5.685012235183888], [4.067409175602521, -0.536439926621362], [2.5656813438853296, 2.7056802874632795], [2.1293914647589034, 4.642559399432756], [3.4127967638900434, -0.24461452723904076], [4.346665070906969, 7.334655181225893], [4.8, 2.0482018725377187], [1.123738605408948, 3.57856337675828], [4.228966634584813, -0.5954109912309717], [0.6031981581824846, 2.2362846421499483], [2.347165380094789, 2.914571054618481], [4.388070857027747, -0.4566421607253668], [2.1979928248704255, 1.0832058812097898], [1.69634605871611, 3.8274151057051444], [1.123738605408948, 3.57856337675828], [1.1747275757821485, 1.7776761397553662], [4.4693615246118945, -0.6333756573025302], [4.53293914647589, -0.9077879799922597], [1.0361298628314102, 4.036083757910509], [2.808234500789304, 0.3071881706888375], [-0.7191638996712912, 7.874957048525548], [2.8260583427385293, 7.660845787800426], [3.9912416391547496, 2.8264553465726032], [2.1123464147922975, 3.1439793965288794], [2.6731182859013067, 0.26597049531016737], [1.1673676524343146, 1.4924116768911837], [4.121463981513893, -0.5859228214803203], [1.4342741983539433, 1.6004923338374206], [4.608830245519423, 1.4005010543426653], [0.9162643526136969, 2.1035411206240853], [1.215956341894931, 4.30182011239446], [1.4872582434657393, 1.5371751490903258], [4.388070857027747, 2.4052170210761252], [0.6031981581824842, 2.4125336286506642], [2.898777190073906, 0.7105093578091037], [0.8945938791669943, 2.1382788324299487], [5.669098065784624, 3.8755737190696062], [6.070148762628594, 0.206906059620394], [1.4568609922766318, 1.4079768509137702], [5.531717813594291, 1.830541309334829], [-1.2471810710311697, 8.743462266446604], [0.6248686316291862, 2.4533251080797505], [3.6748906604483524, 3.124197444178624], [4.893093462225972, 0.4590567027245811], [1.1657585899617324, 1.9134186772549258], [-0.024766020288611745, 3.0296940622407025], [2.3563295257006565, 0.7846312919685801], [5.205353859395272, 0.42210603989954754], [3.8437689822722936, -0.7665448437321474], [3.6748906604483524, 3.1628631330266517], [2.727173091812678, 0.639131207705715], [1.788002622023866, 6.769502536197556], [-0.4057970441397616, 3.2929812953755766], [4.965546187085682, 1.9531004552217803], [-0.046436493735313444, 2.9484252862795244], [2.9371084106171184, 0.20309215819430038], [4.707852582126202, 2.17957929497331], [1.1778166085718507, 1.6671813846379329], [2.683868551208399, 0.5036394358913796], [4.8, 2.0482018725377187], [-2.145131239743776, 4.923667400169843], [4.5024200674231105, 2.3840119264621733], [1.9101959213696633, 1.1695980304271996], [-0.40643138475388163, 3.4467173934044606], [4.259451940886286, 1.0691802220180708], [1.167367652434315, 1.7378040719362537], [1.4872582434657393, 1.700227006989037], [1.6528426011052597, 1.2481771593670978], [4.800000000000001, 2.005728687048103], [-0.04508236022751255, 3.0568571249844774], [2.3439911949358385, 3.5683856892327976], [3.910730421098827, 4.135332344591893], [-0.04508236022751233, 2.9509742434706796], [2.950134753881053, -0.04379815575361201], [4.264382707965382, 0.09902519561511669], [3.106229402999044, 3.7294157226465066], [-0.0247660202886113, 2.989216765708611], [2.1123464147922975, 3.1385816622847216], [1.652915853616594, 1.3022923888888307], [4.259451940886286, 1.0000167752411318], [1.5290924648042203, 4.310240411834759], [2.9371084106171184, 0.20309215819430038], [-2.0852792308147365, 5.03633000521274], [0.2726285121062364, 5.866558412092897], [0.2900620352731951, 2.6721980849212557], [5.108903278970173, 1.6233053913483126], [-0.8239196028969407, 5.030419621266199], [1.8466237141828281, 5.932927653853168], [4.8, 9.658382640793699], [2.950134753881053, 0.22761233374994183], [0.6045522916902855, 2.238833599341103], [2.486105104523525, 7.000796957374647], [0.98431156175139, 4.168919529596836], [-0.8239196028969404, 5.026413025884784], [0.2990310210936111, 2.6018611324034016], [0.6803390498603128, 3.601707913302718], [1.2974476846606788, 3.8503187512101014], [0.8267571383654448, 5.853058068771931], [-0.04482743126273103, 2.87869356247465], [3.2764894324988463, 0.1688700457664547], [-3.767329514700993, 8.66269152688608], [3.222434626587475, -0.03359997239130541], [4.090573653616875, -0.707077359146127], [3.8849164987684937, -0.3938707427571062], [-0.5199470910058632, 5.609801167360874], [5.233045406042793, 1.6406297256739135], [1.5290924648042197, 4.299156029478217], [-0.024766020288611745, 3.0296940622407025], [4.325545325536228, 0.3438574274307671], [0.11249999999999938, 4.508574043613446], [5.15613137186623, -0.3461798183284306], [3.192037375398367, -0.3082514746327818], [0.6031981581824848, 2.469172531793316], [4.0682821864057095, 1.190912989505398], [1.0228149047709139, 5.191509904245847], [0.8267571383654444, 5.861370182915128], [-0.3692763432639352, 3.4022034406151067], [2.285674165981368, 0.40054508668958766], [2.9582532190128603, 2.1211932808694134], [-2.5837485905052917, 9.06294581359145], [-1.9853601848610753, 8.508068840069502], [1.8745211272980575, 1.30411808065442], [0.2862090792517309, 4.825957160184751], [3.0627533820148227, 0.592225673986793], [-0.046436493735313444, 3.103355437229105], [5.205353859395272, 0.7111054161797834], [7.244188130715516, 1.6376585614535513], [1.0228149047709136, 5.216618692355997], [4.074211060786516, 0.932690737783997], [0.4381746991919173, 6.21878271740584], [1.2159563418949306, 3.8495970971114977], [1.4151343263934582, 3.2980753742742706], [3.295606407946904, 0.3243530680371258], [6.0564497139770435, 0.4578939096082548], [-0.7634664185075604, 5.151411845004738], [2.6731182859013067, 0.6900063191517116], [0.34384052085180405, 3.92734467412126], [-0.7966228863999916, 3.730012258845936], [-1.343379060194661, 6.639339649156551], [2.655747377976134, 0.7579169337318432], [0.4741039534911502, 4.183751019358796], [0.8106024824996589, 3.8469096688591344], [4.251583853701234, -0.5122336159174341], [5.433641521339686, 1.2304496491736634], [0.6098506962014656, 2.4407786937191167], [2.958196315583629, 0.42169758533305535], [0.4741039534911502, 4.186979702946429], [3.139194465647819, 7.372362643090272], [6.070148762628594, 3.5839417724562916], [0.34384052085180405, 3.9447824700656744], [-0.024766020288611745, 3.0647459569435602], [0.98431156175139, 4.194028317706987], [3.205063718662302, 0.1665897376282075], [2.249924886746646, 5.685012235183888], [0.2862090792517309, 4.857299154558197], [0.894593879166995, 1.8996977832962885], [1.1883610918565954, 5.485872011543561], [3.956287021404356, 2.025822005622866], [0.020600968107850726, 6.546459784906801], [3.4988012781265745, 3.3537031044984764], [-0.044827431262731476, 2.9919314062894187], [2.4005561755536413, 4.351152383512192], [1.5888434056451888, 2.39808430763285], [3.724642241260815, 1.5993834520787418], [2.0763857262800003, 1.0305473880108813], [3.847522797914033, 1.4837311634639483], [1.8745211272980573, 1.224717120939789], [5.752477202085966, 2.161171990200467], [0.8945938791669943, 2.1382788324299487], [3.233184891894567, 0.19323849790749126], [1.6649738722267118, 1.4776151700223363], [4.677119443346781, 3.3876549706623997], [-0.395982428616346, 3.3114558657725937], [0.6630501671115239, 1.7966830293255285], [3.769512705456301, -0.4277218437869408], [1.4180968883141025, 1.7446822065708214], [4.345201751603508, 1.0003858818660745], [0.8962029416395768, 2.019036011848609], [6.7767023598214795, 2.767717033817918], [5.811238605408949, 10.971775430203346], [0.8945938791669944, 2.1032269377270914], [3.2362710203269964, 0.7662217764717181], [2.4400930748564438, 0.44511030500033666], [2.9431537290560565, 0.43585531382959397], [0.2900620352731951, 2.707249979624113], [4.692497346929079, 0.1733884621768529], [0.8988112239109582, 2.225618323427335], [3.997321854441272, -0.4340312777679376], [3.825695452069068, 2.5535046832593484], [-0.7966228863999916, 3.760365949124784], [3.657528247574082, -0.2364971741873707], [10.728301112968886, 8.129684856516427], [3.2764894324988463, 0.30103814014405605], [-0.2848242861634557, 2.5492067505157534], [3.4972488209905226, 0.0015738001887335429], [6.1942908897012146, 2.957238766835977], [4.499254404787959, 0.8703367099355468], [1.5413130493771106, 1.8775064211159793], [-0.3745113733554064, 3.4274011610281363], [3.1948017901428534, 0.4679448192780584], [4.010305839061002, -0.7770054557893282], [0.8962029416395773, 2.124411983277796], [4.499254404787959, 0.8703367099355468], [4.213823144644389, -0.9389793892200413], [6.898694746008462, 11.99606251173315], [2.333165047686302, 0.9445567230782452], [0.2916710977457775, 2.710278803101915], [-0.5199470910058637, 5.594291058917969], [-8.768059849208484, 13.375959505727446], [-2.2942634638312485, 7.933801838660646], [1.6545249160891764, 1.1890118072921148], [-4.551431127611951, 9.141286955528566], [5.633568473455744, -0.10132487706078885], [0.98431156175139, 4.194028317706987], [-0.7191638996712908, 7.827132937551404], [1.8379957437743928, 4.909616201353765], [10.59608346486909, 12.797203128699032], [0.8267571383654444, 5.861370182915128], [0.21177072258842733, 6.987152345891302], [3.6719651469590926, 9.249671380027745], [7.556358345958318, 7.050258822639192], [-1.450881713265581, 6.478776935187961], [2.9371084106171184, 0.3253638022534422], [2.950134753881053, 0.4292849375237151], [2.478857178818972, 0.8132979823825846], [-0.6936561702575947, 5.275621376822615], [-6.31581424528901, 12.58751873434882], [0.8267571383654444, 5.861370182915128], [3.2050637186623017, 0.07489893978269802], [0.8267571383654444, 5.861370182915128], [-0.12731794307864375, 10.43702448814638], [0.6031981581824843, 2.290261984591522], [1.1883610918565952, 5.528235044517281], [-2.8148523126150256, 10.46228804188221], [3.4127967638900434, -0.15739477788275646], [-0.10237335992179664, 5.223023803192105], [-1.214196617484058, 3.9467738427772825], [-0.10237335992179664, 5.216790596928811], [0.1292714202217442, 5.652827830140181], [-1.4285166649520677, 7.991178815631317], [1.0179268928460212, 6.234650333088625], [-0.10237335992179664, 5.216790596928811], [0.8945206266556602, 1.961891958849074], [-3.7645455411158375, 14.965007331606095], [-2.5837485905052926, 9.084689128297647], [-0.6936561702575943, 5.28281937112232], [0.2900620352731955, 2.6903543069941582], [0.4741039534911502, 4.186979702946429], [1.3593134337794222, 4.90314287805327], [-1.754854225156658, 5.917020061280331], [-0.2760824391735279, 4.896042006953552], [-2.145131239743776, 4.999196591404791], [3.6039665183706195, -0.2719136677062992], [5.97912131397371, 0.7637993559893621], [4.166698914141367, -0.08485018099479191], [4.893093462225972, 0.30016716961001455], [0.2900620352731951, 2.6721980849212557], [-3.6991068907737716, 8.861726191348053], [-0.046436493735313444, 2.9484252862795244], [-0.024766020288611523, 2.9666860241457513], [4.827116471079473, -0.5364894629472379], [-1.2141966174840584, 4.087970934575141], [2.2617088319942518, 0.21893988925413344], [4.18147880047027, 0.19511841248959838], [5.9280348530409075, 8.890736658315404], [4.518788267677348, -0.04154743725049981], [4.44325205928394, -1.007852334000157], [4.290841041213988, -0.17540461714051325], [3.769294411555624, -0.34168885793470505], [4.632658506364657, 0.3070575811701284], [4.388033904986031, -1.2787941827568412], [2.8796602146258485, 0.7169976483376705], [-8.993302706302629, 18.047614948808622], [-0.40804044722646404, 3.32923571550917], [4.388524835038002, -0.22441713017397236], [5.9749455766628685, -0.5105871937116166], [0.2900620352731954, 2.6317207883891633], [0.8945938791669943, 2.1382788324299487], [-16.642526220238658, 19.623895964392474], [-0.046436493735313444, 2.9484252862795244], [0.6031981581824842, 2.4125336286506642], [3.9771730838489914, -0.7757922401415804], [4.536770649420184, -0.20454374589753987], [0.2900620352731951, 2.707249979624113], [-0.4080404472264636, 3.4209265133546793], [4.719384382974241, -0.48143166216084854], [4.544248150302466, -0.5884133974848607], [2.768366006109903, 0.9183434148624996]] The problem is that when i display map_building I see only the map of the first polygon without any point. How to solve it?
I think its perhaps a problem that you dont have a folium map. In the docs of GeoDataFrame.explore() it says the parameter m should be linked to a map. Perhaps first you have to create a map and then use it in the explore command. Similar to this (have not tried this example in lack of geopandas installed) map_building = gdf_building.explore(style_kwds={'color':'black','weight':3,'fillColor':'gray','fillOpacity':0.2}, m=folium.Map(location=[44, -73], zoom_start=5)) I am not aware of the advantages of GeoDataFrame.explore() vs. pure folium. Perhaps, if you do not need it, you can just create a folium map and add the markercluster to it, just like it is done in your linked example. This should work: # create map m = folium.Map(location=[44, -73], zoom_start=5) marker_cluster = MarkerCluster().add_to(m) # add a marker to the cluster, not the map for location in locationlist: marker = folium.Marker(location=location) marker_cluster.add_child(marker) # add layercontrol folium.LayerControl().add_to(m); # show map m
Newly Data Points Hidden Behind the Previously Plotted data in Plotly in the same graph -- plotly.express as px, and plotly.graph_objects as go
I am plotting some data points on a graph. Output: Now, I plotted some ambulance locations (Dataset is in two excel files) Does not work As we can see above, the new plotted data as ambulance location is hidden due to previous plotted data. How do we solve this? Here is my code (Dataset is in two excel files): import plotly.express as px import plotly.graph_objects as go import numpy as np import pandas as pd import matplotlib.pyplot as plt import geopandas as gpd from openpyxl import load_workbook import numpy as np import pandas as pd from mplcursors import cursor import numpy as np; np.random.seed(0) import seaborn as sns; from matplotlib.pyplot import figure import pandas as pd import numpy as np import io file_loc_1 = "AgeGroupData_time_to_treatment.xlsx" df_centroid_Coord = pd.read_excel(file_loc_1, index_col=None, na_values=['NA']) df_centroid_Coord file_loc2Amb = "Ambulance IDs.xlsx" df_station = pd.read_excel(file_loc2Amb, index_col=None, na_values=['NA'], usecols="A:D") df_station.rename(columns={'Longtitude':'x', 'Latitude':'y'}, inplace=True) df_centroid_Coord['Ambulance_Treatment_Time'] = df_centroid_Coord ['Base_TT'] fig = px.scatter(df_centroid_Coord, x="x", y="y", title="Southern Region Centroids", color='Ambulance_Treatment_Time', log_x=True, size_max=60, color_continuous_scale='Reds', range_color=(0.5,2), ) fig.update_traces(marker={'size': 4, 'symbol': 1}) fig.add_trace(go.Scatter(x=df_station['x'], y=df_station['y'], mode='markers+text', text=df_station['Ambulance station name'], textposition='top center', showlegend=True, marker=dict( size=12, symbol=2, color='black' ) ) ) fig.update_layout(width=1000, height=800, paper_bgcolor="LightSteelBlue") fig.show() Working Code: (Dataset is inside code) import plotly.express as px import plotly.graph_objects as go import numpy as np import pandas as pd import matplotlib.pyplot as plt import geopandas as gpd from openpyxl import load_workbook import numpy as np import pandas as pd from io import StringIO import numpy as np; np.random.seed(0) import seaborn as sns; data = ''' x y Base_TT 14.1315559 55.75811117 1.871884861 14.66228957 57.02751992 1.599971112 14.49407157 56.06563489 1.307165278 13.21996788 55.4786748 1.411554445 14.00501286 55.72120854 1.968138334 12.73736102 56.71097302 1.309849028 14.56668525 56.74872925 1.719116945 13.24138764 56.41359089 2.000620417 14.94308088 56.54283706 1.668724723 14.5744739 56.05695327 1.266861528 13.06800876 56.6356658 1.58923875 14.69964193 56.47959746 1.960050139 13.02976922 55.48474858 1.2549575 13.16901029 56.23281882 1.429789167 13.05906805 55.85369617 1.553721944 13.7382052 55.60193648 1.326429166 13.72987233 56.34767237 1.709020555 14.58803736 56.29060354 1.444833472 13.1687123 55.90031115 1.527546805 15.02422205 56.27331725 1.692005 12.91010076 56.24668905 1.090544167 13.2053785 55.45149986 1.438993611 13.37256031 55.61560233 1.632310694 13.26737288 56.19829081 1.869085 13.23818589 55.42501479 1.646565973 14.50187617 56.70356708 1.880334584 14.70189174 56.19209891 1.31886875 13.34482545 56.27124013 1.81529125 12.8785792 55.94059005 1.418415555 12.93136079 55.85161507 1.40189875 12.89164916 55.99473803 1.45182125 13.10496461 56.22286779 1.456101528 15.38666411 56.9824605 1.922860277 13.32430726 56.00137807 1.941762638 13.51541698 56.45311516 1.768057778 15.0413443 57.17170192 2.024309722 12.8606001 55.97622841 1.401550417 13.46749777 56.03019927 1.883145 14.72922746 56.88394501 1.273883612 14.55821135 56.07486606 1.331545972 12.93914018 56.60663923 1.351015694 13.9743559 55.67602686 1.874753334 14.64564524 57.04544092 1.668993889 12.66824186 56.24236656 1.333240139 14.0105656 56.75454085 1.378468056 13.60274138 55.39377504 1.582678472 15.75049672 56.25307533 1.564625833 13.60115485 55.43869065 1.577717361 15.7653804 56.19008971 1.372651944 13.1832264 56.95189695 1.748949583 15.60524952 56.25387079 1.3378925 14.23746798 56.04634612 1.200910417 14.35058672 56.00206764 1.385281667 13.60986899 56.97533616 2.631095833 13.45346163 56.38047383 1.811332917 15.3486521 56.43454959 2.049025556 12.83048942 56.22734439 1.118363333 13.84310479 56.34878109 1.626188472 13.03675856 55.97916057 1.681367638 14.42929903 56.1012924 1.947557639 13.2062902 56.12552876 1.535386944 13.34357303 57.04397378 1.86576 14.03682213 57.00630325 1.720512917 15.18750805 56.64153377 1.769290139 14.47260163 56.46087204 2.272563194 15.44375218 56.24557042 1.541445833 14.46883407 56.73038276 1.778631806 13.10870739 55.81852133 1.51138625 12.86285426 56.80305259 1.40997375 13.49439127 56.98298455 2.250301528 13.70360937 56.16769145 1.196677639 14.08919483 55.52417158 1.591170417 13.55740496 55.77068518 1.535861945 14.18123581 55.66859517 1.996627778 15.23403607 56.25512928 1.548016667 15.70489096 56.40608375 2.047458055 14.37376278 56.55025699 2.067875139 14.502587 56.64966538 2.082391666 13.02508849 56.832684 1.706181111 13.14274787 55.77410524 1.375705139 14.84565179 56.5068175 1.885311528 14.20034005 56.28870701 1.691717361 13.8812731 55.59433892 1.5328325 14.14035572 56.80042421 1.561514584 13.76343201 55.80887467 1.749841528 13.89449225 56.25041347 1.46041875 15.17289149 57.00988892 1.521688473 14.77824552 56.9199763 1.376412361 13.27891612 55.57837586 1.622874028 14.24058672 56.70224862 1.666531805 14.37017794 56.76585965 2.005441666 13.52451801 55.79725389 1.521532778 13.71088665 55.46685563 1.222417361 14.9588848 56.96508403 1.38658 14.82826594 56.75834768 1.485242778 13.1940657 56.38595264 1.792493889 14.84402516 56.90210837 1.235783333 14.46031787 56.18229581 1.681354444 14.63590428 56.35364532 1.650511111 13.34460314 55.52535888 1.473964166 14.62952158 57.00946017 1.6048425 13.71970327 55.68262124 1.564198333 13.13107617 56.34907413 1.663111806 13.48159711 56.89298141 2.3218075 13.81128734 55.80934654 1.869087639 14.15612237 56.82748426 1.526720834 13.25994595 57.06979504 1.953888333 13.36341923 55.45371566 1.683035417 13.53607897 55.91421314 1.662510139 14.84502823 56.65954652 1.808187361 13.5819665 56.40896643 1.723420972 14.10802101 56.09040464 1.322613333 12.59101931 56.18696069 1.285278334 15.22304196 57.13557739 1.861801667 13.93212119 56.63709467 1.879722361 14.04083953 56.20670871 1.426582917 13.20977325 55.70320438 1.036230556 14.50691645 56.31726101 1.512323056 14.04385954 55.43396451 1.518698611 14.62176554 56.14695594 1.436045973 12.94595993 56.48094227 1.284676667 13.43761668 56.76664816 2.052952223 13.4881 55.91363347 1.748883611 13.30499035 56.44143388 1.901664722 14.59756066 56.91952527 1.202981944 13.2574875 55.70389081 1.129174861 13.47396568 56.67724669 2.238806528 12.73718764 56.44136418 1.683367917 13.83580567 55.5220256 1.431533473 14.59492984 57.16207179 1.922699306 14.64495938 57.11730653 1.864279583 14.4312567 57.05363464 1.896975417 13.41144319 55.83179083 1.403585 14.22184539 56.81892717 1.746305417 13.50202498 56.38107009 1.659256389 12.68761018 56.18880561 1.355646945 14.94282338 56.71353347 1.63897125 15.02454518 56.77643118 1.68773 15.57297141 56.25402442 1.414539028 12.964589 56.43631667 1.382988472 13.45825395 55.45494721 1.820561111 14.63191383 56.76690666 1.694100278 13.14103724 55.81002734 1.48003625 15.21769188 56.21922061 1.580773194 14.10257692 55.63210186 1.773493889 13.17668301 56.07117614 1.724194167 15.84692703 56.23448315 1.807886528 13.44350876 55.42779971 1.808543611 15.1857457 56.28216487 1.575294861 13.31802754 56.51349734 1.770477639 12.8935671 56.83951966 1.609666389 15.86386582 56.27030588 2.006792778 13.34532169 55.88483834 1.414042917 14.02227635 55.66743196 1.754815834 15.54234554 56.37096273 1.768464167 14.17727266 55.85726695 1.794488889 13.02668442 55.86216815 1.630864584 14.02130349 56.97024678 1.49533125 14.70175234 56.21006795 1.380317916 13.4056505 56.3618967 1.847926389 14.52330627 56.29935591 1.458468611 15.45924302 56.19160552 1.353913194 13.64578835 55.52007228 1.636670139 15.54272866 56.39791466 1.825142222 13.77315439 57.01298115 2.218526667 15.81485761 56.2436834 1.651075833 13.50857285 55.7970615 1.55940875 13.2405817 55.37112529 1.525214028 13.77433597 56.9770554 1.827475 13.02797412 55.52067001 1.181577917 14.94264555 56.83032305 1.482392778 14.79657364 56.5696361 1.996696389 14.42283094 56.53252583 2.141961944 13.30174382 55.41692656 1.592043888 12.91793298 56.39959593 1.43073125 12.90894568 56.85775049 1.762173056 13.75637136 56.52764363 1.831847639 14.47776811 56.08353697 1.370715 13.22852136 55.64056743 1.272487639 13.58430026 56.34609492 1.82778375 13.49063969 56.66846469 2.12446875 13.10834154 56.8070164 1.948958889 14.04485542 56.0450056 1.215075973 14.13354678 55.66826782 1.915392223 15.50829522 56.24532218 1.578224028 15.13989252 56.99196316 1.574268333 14.38668231 56.75695784 1.967270138 14.37062878 56.73890974 2.157887638 14.17479551 55.97406075 1.204406945 12.94883204 55.51937 1.279889722 13.58529757 56.31914974 1.783001805 13.27981504 55.91988672 1.720325555 13.59881528 56.83145749 2.139697778 13.59915061 56.82247642 2.074659723 13.40296577 56.03836683 1.7947475 14.29429163 55.57045392 1.731105417 12.80578871 56.65827783 1.060809167 14.53129548 56.96421286 1.476721806 13.26306176 55.57815222 1.600958055 14.49804298 56.99103484 1.81823625 13.4719759 55.50904044 1.673925972 13.91167961 56.78067047 1.325035834 14.12918279 56.57573794 1.790625556 14.04255435 55.48787141 1.424878194 14.04189635 56.80869138 1.407714861 14.79691051 56.50674739 1.909035139 13.45649939 55.4998595 1.623631389 14.03861222 55.6495892 1.614189445 14.04342496 55.45193356 1.590481667 13.71133755 56.89556193 2.093918334 14.17229995 56.09085224 1.279623194 13.6748116 56.06854362 1.526770972 12.70417007 56.71935248 1.316963472 13.30434416 56.0909687 1.68737375 14.56792192 56.64092518 2.210113889 13.34417417 55.91178236 1.465459028 14.08469442 56.39572484 1.527290833 13.5484731 55.58186417 1.765038889 14.94294571 56.63267787 1.661855694 14.08079768 55.8835409 1.572365695 13.07503267 55.85394699 1.526148195 13.87707642 56.84325494 1.201778611 14.74834811 56.4797057 2.201030833 14.71780758 56.2190903 1.312630416 14.84688074 56.20134805 1.064184305 13.9756613 56.83511828 1.106081945 13.37482084 56.32555295 1.955326527 13.28496775 55.80313232 1.281214445 13.48814841 55.50025435 1.612392361 13.33917893 56.02853829 1.843289861 13.78947363 55.99783619 1.712659583 13.94116263 56.3137174 1.441294722 12.94495248 55.89677924 1.427171389 13.93984079 56.9426305 1.49043875 15.12394257 57.14469774 1.834394167 13.43537424 56.82053079 1.990671805 14.3348616 55.98401349 1.42602875 15.07312973 56.51587606 1.730443055 13.06228068 55.46729248 1.334419722 15.09067168 57.05489271 1.720642222 15.3000038 56.43467672 2.145260556 14.42783245 56.2001194 1.928752916 12.62154803 56.21451481 1.284935278 14.5647095 56.91941521 1.203295972 12.82352863 56.06545274 1.310144583 15.12128092 56.32716729 1.653416527 15.41977285 57.00032105 1.830578333 13.0108648 56.48199917 1.334979166 13.79627271 56.29442363 1.608296806 13.69390585 55.9788893 1.636546111 13.85415898 56.51959889 1.940849583 13.42994326 56.16451808 1.556413611 13.94165947 56.29575144 1.459196945 13.63833274 55.73566343 1.742808889 13.78097707 56.26731808 1.453552362 ''' df_centroid_Coord = pd.read_csv(StringIO(data), delim_whitespace=True) data = ''' ID "Ambulance station name" Longtitude Latitude 0 1 AImhult 14.128734 56.547992 1 2 Angelhdm 12.870739 56.242114 2 3 Alvesta 14.549503 56.920740 3 4 "Ostra Ljungby" 13.057450 56.188099 4 5 Broby 14.080958 56.254481 5 6 Bromölla 14.466869 56.072272 6 7 Försláv 12.814913 56.350098 7 9 Hasslehdm 13.778234 56.161536 8 10 Haganas 12.556995 56.206016 9 11 Hörby 13.643265 55.849811 10 12 "Halmstad.Vaster" 12.819960 56.674306 ''' df_station = pd.read_csv(StringIO(data), delim_whitespace=True) df_station.rename(columns={'Longtitude':'x', 'Latitude':'y'}, inplace=True) df_centroid_Coord['Ambulance_Treatment_Time'] = df_centroid_Coord ['Base_TT'] fig = px.scatter(df_centroid_Coord, x="x", y="y", title="Southern Region Centroids", color='Ambulance_Treatment_Time', log_x=True, size_max=60, color_continuous_scale='Reds', range_color=(0.5,2), ) fig.update_traces(marker={'size': 4, 'symbol': 1}) fig.add_trace(go.Scatter(x=df_station['x'], y=df_station['y'], mode='markers+text', text=df_station['Ambulance station name'], textposition='top center', showlegend=True, marker=dict( size=8, symbol=2, color='black' ) ) ) fig.show() If I read the same data from externals files, it does not work. Like if I plot the data of map coordniates (from Excel file) and then plot ambulance data (from Excel file), ambulance symbol in the diamond shape appears behind the map. It is not showing on the front. When dataset inside the code, the ambulance data is shown and not hidden Increased ambulance size - When dataset inside the code, the ambulance data is shown and not hidden
I think that there is no problem, I tried the same code with 2 Excel files with your data and I got the plot below: import plotly.express as px import plotly.graph_objects as go from openpyxl import load_workbook import pandas as pd import numpy as np import io file_loc_1 = "AgeGroupData_time_to_treatment.xlsx" df_centroid_Coord = pd.read_excel(file_loc_1, index_col=None, na_values=['NA']) df_centroid_Coord file_loc2Amb = "Ambulance IDs.xlsx" df_station = pd.read_excel(file_loc2Amb, index_col=None, na_values=['NA'], usecols="A:D") df_station.rename(columns={'Longtitude':'x', 'Latitude':'y'}, inplace=True) df_centroid_Coord['Ambulance_Treatment_Time'] = df_centroid_Coord ['Base_TT'] fig = px.scatter(df_centroid_Coord, x="x", y="y", title="Southern Region Centroids", color='Ambulance_Treatment_Time', log_x=True, size_max=60, color_continuous_scale='Reds', range_color=(0.5,2), ) fig.update_traces(marker={'size': 4, 'symbol': 1}) fig.add_trace(go.Scatter(x=df_station['x'], y=df_station['y'], mode='markers+text', text=df_station['Ambulance station name'], textposition='top center', showlegend=True, marker=dict( size=12, symbol=2, color='black' ) ) ) fig.update_layout(width=1000, height=800, paper_bgcolor="LightSteelBlue") fig.show()
Folium Choropleth Highlight map elements is not working
This is my first question so hopefully, I'm asking it in a way that makes sense. If not, please correct me. I want to use the highlight parameter in folium.Choropleth to achieve this sort of behaviour on mouse hover: but it's not working. I noticed one strange thing: I also have folium.features.GeoJsonTooltip in my code and if I disable it, highlighting works. But when it's enabled, highlighting does not work. When folium.features.GeoJsonTooltip is enabled, the code compiles without errors but it's not highlighting countries as it should. All other functionalities work as expected. folium.Choropleth( geo_data=df1, name="choropleth", data=df3, columns=["Country", "Estimate_UN"], key_on="feature.properties.name", fill_color="YlGnBu", fill_opacity=0.8, line_opacity=0.5, legend_name="GDP Per Capita (in EUR)", bins=bins, highlight=True ).add_to(my_map) Here's my full code: import folium import pandas import geopandas pandas.set_option('display.max_columns',25) pandas.set_option('display.width',2000) pandas.set_option('display.max_rows',300) url = 'http://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita' tables1 = pandas.read_html(url, match='Country/Territory') df1 = tables1[0] # changes data from List to DataFrame # makes it single index df1.columns = ['Country', 'Region', 'Estimate_IMF', 'Year1', 'Estimate_UN', 'Year2', 'Estimate_WB', 'Year3'] # makes it two columns only (Country, Estimate_UN) df1 = df1.drop(columns=['Region', 'Year1', 'Year2', 'Year3', 'Estimate_IMF', 'Estimate_WB']) df1['Country'] = df1['Country'].map(lambda x: x.rstrip('*')) df1['Country'] = df1['Country'].map(lambda x: x.strip()) df1['Country'] = df1['Country'].str.replace('United States', 'United States of America') df1['Country'] = df1['Country'].str.replace('DR Congo', 'Dem. Rep. Congo') df1['Country'] = df1['Country'].str.replace('Central African Republic', 'Central African Rep.') df1['Country'] = df1['Country'].str.replace('South Sudan', 'S. Sudan') df1['Country'] = df1['Country'].str.replace('Czech Republic', 'Czechia') df1['Country'] = df1['Country'].str.replace('Bosnia and Herzegovina', 'Bosnia and Herz.') df1['Country'] = df1['Country'].str.replace('Ivory Coast', """Côte d'Ivoire""") df1['Country'] = df1['Country'].str.replace('Dominican Republic', 'Dominican Rep.') df1['Country'] = df1['Country'].str.replace('Eswatini', 'eSwatini') df1['Country'] = df1['Country'].str.replace('Equatorial Guinea', 'Eq. Guinea') df1.drop(df1[df1['Estimate_UN'] == '—'].index, inplace = True) df1['Estimate_UN'] = df1['Estimate_UN'].apply(lambda g:int(str(g))) ### --- Change 'GDP Per Capita' values in GeoJsonToolTip from format of 12345.0 (USD) to €11,604 --- ### df2 = df1.copy() df2['Estimate_UN'] = df2['Estimate_UN'].apply(lambda g:g*0.94) # Convert USD to EUR df3 = df2.copy() df2['Estimate_UN'] = df2['Estimate_UN'].apply(lambda g:str(int(g))) df2['Estimate_UN'] = '€' + df2['Estimate_UN'].astype(str) length = (df2['Estimate_UN'].str.len()) df2.loc[length == 7, 'Estimate_UN'] = df2[['Estimate_UN']].astype(str).replace(r"(\d{3})(\d+)", r"\1,\2", regex=True) df2.loc[length == 6, 'Estimate_UN'] = df2[['Estimate_UN']].astype(str).replace(r"(\d{2})(\d+)", r"\1,\2", regex=True) df2.loc[length == 5, 'Estimate_UN'] = df2[['Estimate_UN']].astype(str).replace(r"(\d{1})(\d+)", r"\1,\2", regex=True) ### --- Create map --- ### world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres')) df1 = world.merge(df1, how='left', left_on=['name'], right_on=['Country']) df1 = df1.dropna(subset=['Estimate_UN']) df2 = world.merge(df2, how='left', left_on=['name'], right_on=['Country']) df2 = df2.dropna(subset=['Estimate_UN']) df3 = world.merge(df3, how='left', left_on=['name'], right_on=['Country']) df3 = df3.dropna(subset=['Estimate_UN']) my_map = folium.Map(location=(39.22753573470106, -3.650093262568073), zoom_start=2, tiles = 'https://server.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attr = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye,Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community', min_zoom=2, min_lot=-179, max_lot=179, min_lat=-65, max_lat=179, max_bounds=True) ### --- Add tooltip --- ### gdp = folium.FeatureGroup(name="GDP") gdp.add_child(folium.GeoJson(data=df2, tooltip = folium.features.GeoJsonTooltip( fields=['Country','Estimate_UN'], aliases=['Country:','GDP Per Capita:'], style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;"), localize = True), style_function= lambda y:{ 'stroke':'false', 'opacity':'0', })) ### --- Color countries --- ### bins = [100,1000,5000,10000,20000,35000,50000,112000] folium.Choropleth( geo_data=df1, name="choropleth", data=df3, columns=["Country", "Estimate_UN"], key_on="feature.properties.name", fill_color="YlGnBu", fill_opacity=0.8, line_opacity=0.5, legend_name="GDP Per Capita (in EUR)", bins=bins, highlight=True ).add_to(my_map) my_map.add_child(gdp) my_map.save('index.html') I'm looking forward to your suggestions on why GeoJsonTooltip is stopping the highlight parameter from working!
My understanding is that folium.Choropleth() has a highlighting feature, but no popup or tooltip feature. If you want to use the tooltip and popup functions, use folium.Geojson(). I will respond with a df3 of the data you presented. I have implemented my own color map for color coding. The index of the color map is modified according to the number of colors. See this for more information about our own colormaps. The tooltip is introduced as you set it up. We have also added a pop-up feature. You can add supplementary information. If you don't need it, please delete it. The color fill is specified by the style function, which gets the color name from the estimated value for the previously specified colormap function. At the same time, a highlight function is added to change the transparency of the map drawing. The basic code can be found here. import folium from folium.features import GeoJsonPopup, GeoJsonTooltip import branca bins = [5000,25000,45000,65000,112000] my_map = folium.Map(location=(39.22753573470106, -3.650093262568073), zoom_start=2, tiles = 'https://server.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attr = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye,Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community', min_zoom=2, min_lot=-179, max_lot=179, min_lat=-65, max_lat=179, max_bounds=True) colormap = branca.colormap.LinearColormap( vmin=df3['Estimate_UN'].quantile(0.0), vmax=df3['Estimate_UN'].quantile(1), colors=["red", "orange", "lightblue", "green", "darkgreen"], caption="Original Colormap", index=bins ) tooltip = folium.features.GeoJsonTooltip( fields=['Country','Estimate_UN'], aliases=['Country:','GDP Per Capita:'], style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;"), localize=True) popup = GeoJsonPopup( fields=['Country','Estimate_UN'], aliases=['Country:','GDP Per Capita:'], localize=True, labels=True, style="background-color: yellow;", ) folium.GeoJson(data=df3, tooltip=tooltip, popup=popup, style_function= lambda y:{ "fillColor": colormap(y["properties"]["Estimate_UN"]), 'stroke':'false', 'opacity': 0.4 }, highlight_function=lambda x: {'fillOpacity': 0.8}, ).add_to(my_map) colormap.add_to(my_map) # my_map.save('index.html') my_map
How can I show specific data on a mapbox from plotly express
I have linked the data and chosen the right parameters, but the map is empty. I have also looked at the documentation of https://plotly.com/python/mapbox-county-choropleth/ , but that doesn't help me. Here is the code I made: # IMPORTEREN VAN LIBARIES import plotly.express as px import pandas as pd import plotly import plotly.offline as po from urllib.request import urlopen import json # LEZEN VAN DATASET fietsdata = pd.read_excel('Fietsdata.xlsx') with urlopen('http://larscuny.info/projecten/dip/data/countries.json') as response: countries = json.load(response) fig = px.choropleth_mapbox( fietsdata, geojson=countries, locations='country', color='total_profit', color_continuous_scale="Viridis", mapbox_style="open-street-map", # carto-positron ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) # LAYOUT VAN DE VISUALISATIE fig.update_layout( font_family="Poppins", font_color="#002072", title_font_family="Poppins", title_font_color="#002072", legend_title="Gender", legend_title_font_color="#002072", ) # OPEN VISUALISATIE po.plot(fig, filename="total-revenue.html") And the dataset I used: country state total_costs total_revenue total_profit Canada Brittish Columbia 360.00 950.00 590.00 Australia New South Wales 1,035.00 2,401.00 1366.00 United States Oregon 405.00 929.00 524.00 I hope someone can help me
your data preparation does not consider that country is not in the geojson as a property. Have used geopandas for prep and joining. NB - USA is lost as join keys are inconsistent you should consider where you want to be the center and the zoom level # IMPORTEREN VAN LIBARIES import plotly.express as px import pandas as pd import geopandas as gpd import plotly import plotly.offline as po from urllib.request import urlopen import json # LEZEN VAN DATASET # fietsdata = pd.read_excel('Fietsdata.xlsx') fietsdata =pd.DataFrame({'country': ['Canada', 'Australia', 'United States'], 'state': ['Brittish Columbia', 'New South Wales', 'Oregon'], 'total_costs': ['360.00', '1,035.00', '405.00'], 'total_revenue': ['950.00', '2,401.00', '929.00'], 'total_profit': [590.0, 1366.0, 524.0]}) with urlopen('http://larscuny.info/projecten/dip/data/countries.json') as response: countries = json.load(response) # use geopandas and join data together gdf = gpd.GeoDataFrame.from_features(countries) gdf = gdf.merge(fietsdata, left_on="ADMIN", right_on="country") fig = px.choropleth_mapbox( gdf, geojson=gdf.geometry, locations=gdf.index, color='total_profit', color_continuous_scale="Viridis", center={"lat": gdf.iloc[0].geometry.centroid.y, "lon": gdf.iloc[0].geometry.centroid.x}, zoom=1, mapbox_style="open-street-map", # carto-positron ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) # LAYOUT VAN DE VISUALISATIE fig.update_layout( font_family="Poppins", font_color="#002072", title_font_family="Poppins", title_font_color="#002072", legend_title="Gender", legend_title_font_color="#002072", ) # OPEN VISUALISATIE fig
Plotly Choropleth Map Not Showing Up
I'm trying to display a Plotly Choropleth Map in Jupyter Notebooks (I'm a beginner with this type of stuff) and for some reason it won't display correctly. The csv file I am using for it can be found here: https://www.kaggle.com/ajaypalsinghlo/world-happiness-report-2021 Here is the code leading up to the choropleth: # here we're assigning the hover data columns to use for our choropleth map below hover_data_cols_df = ['Country', 'Life Ladder', 'Log GDP per capita', 'Social support', 'Healthy life expectancy at birth', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption'] df.groupby('Year').Country.count() and here is the code for the actual choropleth: choropleth_map = px.choropleth(df, locations="Country", color='Life Ladder', hover_name = 'Life Ladder', hover_data = hover_data_cols_df, color_continuous_scale = px.colors.sequential.Oranges, animation_frame="Year" ).update_layout (title_text = 'World Happiness Index - year wise data', title_x = 0.5,); iplot(choropleth_map) I'm not getting any error messages attached to it currently, however when I check my console log on my browser, I do find this error: Wolrd-Happiness-Report.ipynb:1 Uncaught ReferenceError: require is not defined at <anonymous>:1:17 at t.attachWidget (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at t.insertWidget (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at x._insertOutput (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at x.onModelChanged (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at m (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at Object.l [as emit] (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at e.emit (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at c._onListChanged (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) at m (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2) I'm not too sure if this is related or not! Thanks all!
Your task requires a setting that associates a country name with a country on the map. It requires that the location mode be the country name. import pandas as pd df = pd.read_csv('./data/world-happiness-report.csv', sep=',') df.sort_values('year', ascending=True, inplace=True) hover_data_cols_df = ['Country name', 'year', 'Life Ladder', 'Log GDP per capita', 'Social support', 'Healthy life expectancy at birth', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption'] import plotly.express as px fig = px.choropleth(df, locations="Country name", locationmode='country names', color='Life Ladder', hover_name = 'Life Ladder', hover_data = hover_data_cols_df, color_continuous_scale = px.colors.sequential.Oranges, animation_frame="year" ) fig.update_layout (title_text = 'World Happiness Index - year wise data', title_x = 0.5,); fig.show()