如何使用Julia和lot.jl在主题地图中使用连续的色标
我想使用plots.jl
绘制主题地图(Choropleth)。 dataframe df
i使用有3列:几何
,value1
和value2
。列几何
是元素类型polygon
的(使用shapefile
软件包读取数据)。列value1
和value2
属于元素类型int64
,带有值> = 0
。 LightBlue)使用LightBlue
多边形创建地图,而没有问题(并且没有Colorbar到侧面)。
但是,我想要value2
列以基于colormap/colorscheme(例如viridis
)的多边形(形状)上色(形状),并用相应的配色杆作为传说。
有什么建议如何做?
绘图(SHP.几何,Palette = Colorschemes.viridis)
确实绘制了彩色地图,但是尚不清楚该颜色基于哪种变量。我想手动指定该(value2
列),并添加配色栏。
这是可重复性和可能解决方案的完整代码。
# Load modules
using CSV
using DataFrames
using Dates
using Shapefile
using ZipFile
using Plots
using ColorSchemes
# Create directorties
for dir in ["data", "downloads", "shapefiles"]
path = joinpath(pwd(), dir)
if !ispath(path)
mkpath(path)
end
end
# Download shapefiles
zip_url = "https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/wijk-en-buurtstatistieken/wijkbuurtkaart_2021_v1.zip"
zip_loc = joinpath(pwd(), "downloads", split(zip_url, "/")[end])
if ~isfile(zip_loc)
download(zip_url, zip_loc)
end
# Extract shape files
r = ZipFile.Reader(zip_loc)
for f in r.files
file_name = split(f.name, "/")[end]
if startswith(file_name, "gemeente")
println("Extracting: $(file_name)")
open(joinpath(pwd(), "shapefiles", file_name), "w") do io
write(io, read(f))
end
end
end
# Read shapefiles
name_shapefile = "gemeente_2021_v1.shp"
path_shapefile = joinpath(pwd(), "shapefiles", name_shapefile)
table = Shapefile.Table(path_shapefile)
df = table |> DataFrame
row_filter = df.H2O .== "NEE"
df = df[row_filter, [:geometry]]
# Add columns value1 and value2
df.value1 = rand(1:100, nrow(df))
df.value2 = rand(1:5000, nrow(df))
# Plot map
plot(df.geometry, palette = ColorSchemes.viridis, axis=false, ticks=false, size=(500, 600))
I would like to plot a thematic map (choropleth) using Plots.jl
. The DataFrame df
I use has 3 columns: geometry
, value1
and value2
. The column geometry
is of element type Polygon
(data was read using the Shapefile
package). The columns value1
and value2
are of element type Int64
with values >= 0.
plot(df.geometry, color = :lightblue)
creates a map with lightblue
polygons without issues (and without colorbar to the side).
However, I would like the value2
column to color the polygons (shapes) based on a colormap/colorscheme like viridis
with a corresponding colorbar to the side as legend.
Any suggestions how to do this?
plot(shp.geometry, palette = ColorSchemes.viridis)
does plot a coloured map, however it is not clear on which variable this colours are based. I would like to specify that manually (value2
column) and have the colorbar added.
Here's the full code for reproducibility and possible soltuions.
# Load modules
using CSV
using DataFrames
using Dates
using Shapefile
using ZipFile
using Plots
using ColorSchemes
# Create directorties
for dir in ["data", "downloads", "shapefiles"]
path = joinpath(pwd(), dir)
if !ispath(path)
mkpath(path)
end
end
# Download shapefiles
zip_url = "https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/wijk-en-buurtstatistieken/wijkbuurtkaart_2021_v1.zip"
zip_loc = joinpath(pwd(), "downloads", split(zip_url, "/")[end])
if ~isfile(zip_loc)
download(zip_url, zip_loc)
end
# Extract shape files
r = ZipFile.Reader(zip_loc)
for f in r.files
file_name = split(f.name, "/")[end]
if startswith(file_name, "gemeente")
println("Extracting: $(file_name)")
open(joinpath(pwd(), "shapefiles", file_name), "w") do io
write(io, read(f))
end
end
end
# Read shapefiles
name_shapefile = "gemeente_2021_v1.shp"
path_shapefile = joinpath(pwd(), "shapefiles", name_shapefile)
table = Shapefile.Table(path_shapefile)
df = table |> DataFrame
row_filter = df.H2O .== "NEE"
df = df[row_filter, [:geometry]]
# Add columns value1 and value2
df.value1 = rand(1:100, nrow(df))
df.value2 = rand(1:5000, nrow(df))
# Plot map
plot(df.geometry, palette = ColorSchemes.viridis, axis=false, ticks=false, size=(500, 600))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try