如何使用Julia和lot.jl在主题地图中使用连续的色标

发布于 2025-02-01 10:56:12 字数 2201 浏览 3 评论 0原文

我想使用plots.jl绘制主题地图(Choropleth)。 dataframe df i使用有3列:几何value1value2。列几何是元素类型polygon的(使用shapefile软件包读取数据)。列value1value2属于元素类型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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2025-02-08 10:56:12

尝试

plot(df.geometry, fill = palette(:viridis), 
                  fill_z = reshape(df.value2, 1, nrow(df)), 
                  axis=false,
                  ticks=false,
                  size=(500, 600))

Try

plot(df.geometry, fill = palette(:viridis), 
                  fill_z = reshape(df.value2, 1, nrow(df)), 
                  axis=false,
                  ticks=false,
                  size=(500, 600))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文