首页 > 解决方案 > 在散点图上绘制纬度和经度

问题描述

我想制作一个靠近在一起的 latlong 点的散点图(即,在同一个城市)。

到目前为止,我已经写过:

import plotly.express as px
import pandas as pd


filename = "short_rtv.pkl"
df = pd.read_pickle(filename)

fig = px.scatter(df, x="Long", y="Lat", hover_data=["Request"])
fig.write_html("plot.html")

但这给我带来了一个看起来很奇怪的散点图:

在此处输入图像描述

而我希望得到更多类似的东西:

在此处输入图像描述

第二张图像是使用 plotly 的图形对象/地图特征生成的;但是,我不想要地图,我想要散点图。有没有可能实现?

标签: pythonplotly

解决方案


  • 已使用英国行政区域作为样本数据
  • 设置宽度高度以防止x被拉伸很重要
  • 从您的图像看来,longlat列是字符串而不是浮点数
dfso = pd.DataFrame({"areaName": ["Aberdeen City", "Aberdeenshire", "Antrim and Newtownabbey", "Ards and North Down", "Argyll and Bute", "Blaenau Gwent", "Bridgend", "Caerphilly", "Cardiff", "Carmarthenshire", "Ceredigion", "Derby", "Devon", "Dorset", "Dumfries and Galloway", "East Sussex", "Falkirk", "Fermanagh and Omagh", "Flintshire", "Halton", "Highland", "Isle of Anglesey", "Isle of Wight", "Kingston upon Hull, City of", "Lincolnshire", "Lisburn and Castlereagh", "Merthyr Tydfil", "Mid and East Antrim", "Monmouthshire", "Moray", "Na h-Eileanan Siar", "Neath Port Talbot", "Newport", "Norfolk", "North Lincolnshire", "Orkney Islands", "Peterborough", "Portsmouth", "Powys", "Rhondda Cynon Taf", "Scottish Borders", "Shetland Islands", "Shropshire", "Somerset", "Suffolk", "Swindon", "Thurrock", "Torbay", "Torfaen", "Vale of Glamorgan", "Walsall", "West Sussex", "Windsor and Maidenhead", "Wrexham", "Armagh City, Banbridge and Craigavon", "Barking and Dagenham", "Barnet", "Barnsley", "Belfast", "Bexley", "Cambridgeshire", "Causeway Coast and Glens", "Coventry", "Cumbria", "Darlington", "Derbyshire", "Doncaster", "Dudley", "East Riding of Yorkshire", "Essex", "Fife", "Gateshead", "Gwynedd", "Hampshire", "Harrow", "Havering", "Herefordshire, County of", "Hertfordshire", "Hillingdon", "Inverclyde", "Kent", "Leicestershire", "Medway", "Mid Ulster", "Middlesbrough", "Milton Keynes", "Newry, Mourne and Down", "North Somerset", "North Yorkshire", "Northamptonshire", "Nottinghamshire", "Oxfordshire", "Pembrokeshire", "Plymouth", "Redbridge", "Redcar and Cleveland", "Rotherham", "Rutland", "Sandwell", "Sheffield", "South Lanarkshire", "Southend-on-Sea", "Staffordshire", "Sunderland", "Surrey", "Wiltshire", "Worcestershire", "Bath and North East Somerset", "Birmingham", "Bournemouth, Christchurch and Poole", "Brent", "Brighton and Hove", "Bristol, City of", "Bromley", "Buckinghamshire", "Conwy", "Cornwall and Isles of Scilly", "County Durham", "Croydon", "Denbighshire", "Derry City and Strabane", "Ealing", "East Ayrshire", "East Dunbartonshire", "Enfield", "Gloucestershire", "Greenwich", "Hackney and City of London", "Haringey", "Hartlepool", "Hounslow", "Kingston upon Thames", "Lewisham", "Merton", "Newham", "North Lanarkshire", "Perth and Kinross", "Richmond upon Thames", "Slough", "Solihull", "South Ayrshire", "South Gloucestershire", "South Tyneside", "Southampton", "St. Helens", "Stockton-on-Tees", "Stoke-on-Trent", "Sutton", "Swansea", "Telford and Wrekin", "Wakefield", "Waltham Forest", "Warrington", "Warwickshire", "West Berkshire", "West Lothian", "Westminster", "Wirral", "Wolverhampton", "York", "Angus", "Bedford", "Blackburn with Darwen", "Blackpool", "Bolton", "Bracknell Forest", "Bradford", "Bury", "Calderdale", "Camden", "Central Bedfordshire", "Cheshire East", "Cheshire West and Chester", "City of Edinburgh", "Clackmannanshire", "Dundee City", "East Lothian", "East Renfrewshire", "Glasgow City", "Hammersmith and Fulham", "Islington", "Kensington and Chelsea", "Kirklees", "Knowsley", "Lambeth", "Lancashire", "Leeds", "Leicester", "Liverpool", "Luton", "Manchester", "Midlothian", "Newcastle upon Tyne", "North Ayrshire", "North East Lincolnshire", "North Tyneside", "Northumberland", "Nottingham", "Oldham", "Reading", "Renfrewshire", "Rochdale", "Salford", "Sefton", "Southwark", "Stirling", "Stockport", "Tameside", "Tower Hamlets", "Trafford", "Wandsworth", "West Dunbartonshire", "Wigan", "Wokingham"], 
                     "long": [-2.20398, -2.79208, -6.1776, -5.64568, -5.22114, -3.18592, -3.61375, -3.19753, -3.22209, -4.2111, -3.94993, -1.47189, -3.65698, -2.41467, -4.02863, 0.334559, -3.83619, -7.5271, -3.18248, -2.68853, -4.66103, -4.32298, -1.33366, -0.30382, -0.23884, -6.03545, -3.36425, -6.14645, -2.9028, -3.20202, -6.65722, -3.74638, -2.89769, 0.964714, -0.52407, -2.90025, -0.26874, -1.07006, -3.43531, -3.41359, -2.85869, -1.37344, -2.73667, -3.29595, 1.04919, -1.73367, 0.334861, -3.55523, -3.05101, -3.39801, -1.97044, -0.45933, -0.67541, -2.99203, -6.43455, 0.129479, -0.21819, -1.54925, -5.92535, 0.146212, -0.23506, -6.5996, -1.51908, -2.90213, -1.56835, -1.6021, -1.10894, -2.10171, -0.66195, 0.54106, -2.98251, -1.6868, -3.77715, -1.24735, -0.33598, 0.235368, -2.73931, -0.27699, -0.44182, -4.75387, 0.72137, -1.3779, 0.563174, -6.8889, -1.21099, -0.7407, -6.08891, -2.75438, -1.55032, -0.86668, -1.00656, -1.29146, -4.90818, -4.11297, 0.070085, -1.00608, -1.28651, -0.6263, -2.00771, -1.54254, -3.83272, 0.706923, -2.02756, -1.43344, -0.33911, -1.92661, -2.20935, -2.48654, -1.88141, -1.84807, -0.27568, -0.15079, -2.57742, 0.039246, -0.80569, -3.74646, -4.64254, -1.8405, -0.07761, -3.34761, -7.42064, -0.3141, -4.29057, -4.22417, -0.08144, -2.15235, 0.050093, -0.06045, -0.1067, -1.27018, -0.37855, -0.28367, -0.01734, -0.18868, 0.027261, -3.9514, -3.88484, -0.28914, -0.57617, -1.71558, -4.72899, -2.46922, -1.44679, -1.39952, -2.7031, -1.30664, -2.15888, -0.17226, -3.96723, -2.48941, -1.42092, -0.0188, -2.56167, -1.56874, -1.27364, -3.60909, -0.15295, -3.06701, -2.12746, -1.07375, -2.8921, -0.45463, -2.4636, -3.02199, -2.47952, -0.73363, -1.87389, -2.3088, -1.96182, -0.16291, -0.47754, -2.29299, -2.70298, -3.27826, -3.75316, -2.97095, -2.72435, -4.3606, -4.21479, -0.21735, -0.10989, -0.18977, -1.78085, -2.83297, -0.11385, -2.46091, -1.50736, -1.1304, -2.91364, -0.42319, -2.23359, -3.11738, -1.65297, -4.7246, -0.13911, -1.50923, -2.07523, -1.16667, -2.05274, -0.99071, -4.56834, -2.14784, -2.38485, -2.99177, -0.07308, -4.32595, -2.12467, -2.077, -0.03647, -2.36572, -0.20021, -4.52074, -2.57247, -0.89935], 
                     "lat": [57.166969, 57.234692, 54.693859, 54.564091, 56.28944, 51.753639, 51.5606, 51.650009, 51.502541, 51.894951, 52.297951, 52.914639, 50.725578, 50.79697, 55.09621, 50.938049, 56.000751, 54.385208, 53.21471, 53.33424, 57.586571, 53.279308, 50.671291, 53.769199, 53.111019, 54.49752, 51.748581, 54.86462, 51.778271, 57.476799, 58.199379, 51.644501, 51.58231, 52.67123, 53.58643, 58.94334, 52.59214, 50.807999, 52.34864, 51.621849, 55.525951, 60.504951, 52.62212, 51.071819, 52.253849, 51.577629, 51.509979, 50.470921, 51.69836, 51.44836, 52.60503, 50.95134, 51.480339, 53.001671, 54.3867, 51.545551, 51.611069, 53.525768, 54.59853, 51.458221, 52.349659, 55.039619, 52.41423, 54.636379, 54.535339, 53.078579, 53.52697, 52.495129, 53.881119, 51.809818, 56.231121, 54.931198, 52.89883, 51.044739, 51.594669, 51.565189, 52.081539, 51.808788, 51.53664, 55.900299, 51.192951, 52.68779, 51.44772, 54.552731, 54.54467, 52.072411, 54.149529, 51.39706, 54.093712, 52.31105, 53.128502, 51.771549, 51.855129, 50.404942, 51.58588, 54.56752, 53.395531, 52.667648, 52.514771, 53.40358, 55.60453, 51.549171, 52.909302, 54.857189, 51.268028, 51.328831, 52.212872, 51.356041, 52.484039, 50.74609, 51.564381, 50.8465, 51.471149, 51.372662, 51.769661, 53.147388, 50.450218, 54.685131, 51.365971, 53.088329, 54.80904, 51.524422, 55.496738, 55.95829, 51.648891, 51.81971, 51.46394, 51.55492, 51.587711, 54.67614, 51.462379, 51.39296, 51.442299, 51.41058, 51.53149, 55.868141, 56.575279, 51.44035, 51.503502, 52.431, 55.23008, 51.54673, 54.969879, 50.9212, 53.458618, 54.556911, 53.017071, 51.357552, 51.658058, 52.714169, 53.659222, 51.594608, 53.391628, 52.179771, 51.445591, 55.8992, 51.512211, 53.374779, 52.597881, 53.96582, 56.725182, 52.196281, 53.700802, 53.82164, 53.584492, 51.411301, 53.843819, 53.593102, 53.720482, 51.543049, 51.999031, 53.167931, 53.163361, 55.911201, 56.147839, 56.4776, 55.94207, 55.74868, 55.876492, 51.487331, 51.54546, 51.496479, 53.64233, 53.437881, 51.464451, 53.86216, 53.822731, 52.635921, 53.408298, 51.891022, 53.470089, 55.82111, 55.021011, 55.72789, 53.52327, 55.028961, 55.300369, 52.954189, 53.557678, 51.453018, 55.848621, 53.60741, 53.479271, 53.482101, 51.465939, 56.249531, 53.391159, 53.478668, 51.515541, 53.41671, 51.4524, 56.0014, 53.51445, 51.422958]})

px.scatter(dfso, x="long", y="lat", hover_name="areaName", width=400, height=400)

在此处输入图像描述


推荐阅读