• Stars
    star
    512
  • Rank 86,323 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ridge plots of ridges

ridge_map

Build status

Ridge plots of ridges

A library for making ridge plots of... ridges. Choose a location, get an elevation map, and tinker with it to make something beautiful. Heavily inspired from Zach Cole's beautiful art, Jake Vanderplas' examples, and Joy Division's 1979 album "Unknown Pleasures".

Uses matplotlib, SRTM.py, numpy, and scikit-image (for lake detection).

Installation

Available on PyPI:

pip install ridge_map

Or live on the edge and install from github with

pip install git+git://github.com/colcarroll/ridge_map.git

Want to help?

  • I feel like I am missing something easy or obvious with lake/road/river/ocean detection, but what I've got gets me most of the way there. If you hack on the RidgeMap.preprocessor method and find something nice, I would love to hear about it!
  • Did you make a cool map? Open an issue with the code and I will add it to the examples.

Examples

The API allows you to download the data once, then edit the plot yourself, or allow the default processor to help you.

New Hampshire by default

Plotting with all the defaults should give you a map of my favorite mountains.

from ridge_map import RidgeMap

RidgeMap().plot_map()

png

Download once and tweak settings

First you download the elevation data to get an array with shape (num_lines, elevation_pts), then you can use the preprocessor to automatically detect lakes, rivers, and oceans, and scale the elevations. Finally, there are options to style the plot

rm = RidgeMap((11.098251,47.264786,11.695633,47.453630))
values = rm.get_elevation_data(num_lines=150)
values=rm.preprocess(
    values=values,
    lake_flatness=2,
    water_ntile=10,
    vertical_ratio=240)
rm.plot_map(values=values,
            label='Karwendelgebirge',
            label_y=0.1,
            label_x=0.55,
            label_size=40,
            linewidth=1)

png

Plot with colors!

If you are plotting a town that is super into burnt orange for whatever reason, you can respect that choice.

rm = RidgeMap((-97.794285,30.232226,-97.710171,30.334509))
values = rm.get_elevation_data(num_lines=80)
rm.plot_map(values=rm.preprocess(values=values, water_ntile=12, vertical_ratio=40),
            label='Austin\nTexas',
            label_x=0.75,
            linewidth=6,
            line_color='orange')

png

Plot with even more colors!

The line color accepts a matplotlib colormap, so really feel free to go to town.

rm = RidgeMap((-123.107300,36.820279,-121.519775,38.210130))
values = rm.get_elevation_data(num_lines=150)
rm.plot_map(values=rm.preprocess(values=values, lake_flatness=3, water_ntile=50, vertical_ratio=30),
            label='The Bay\nArea',
            label_x=0.1,
            line_color = plt.get_cmap('spring'))

png

Plot with custom fonts and elevation colors!

You can find a good font from Google, and then get the path to the ttf file in the github repo.

If you pass a matplotlib colormap, you can specify kind="elevation" to color tops of mountains different from bottoms. ocean, gnuplot, and bone look nice.

from ridge_map import FontManager

font = FontManager('https://github.com/google/fonts/blob/main/ofl/uncialantiqua/UncialAntiqua-Regular.ttf?raw=true')
rm = RidgeMap((-156.250305,18.890695,-154.714966,20.275080), font=font.prop)

values = rm.get_elevation_data(num_lines=100)
rm.plot_map(values=rm.preprocess(values=values, lake_flatness=2, water_ntile=10, vertical_ratio=240),
            label="Hawai'i",
            label_y=0.85,
            label_x=0.7,
            label_size=60,
            linewidth=2,
            line_color=plt.get_cmap('ocean'),
            kind='elevation')

png

How do I find a bounding box?

I have been using this website. I find an area I like, draw a rectangle, then copy and paste the coordinates into the RidgeMap constructor.

rm = RidgeMap((-73.509693,41.678682,-73.342838,41.761581))
values = rm.get_elevation_data()
rm.plot_map(values=rm.preprocess(values=values, lake_flatness=2, water_ntile=2, vertical_ratio=60),
            label='Kent\nConnecticut',
            label_y=0.7,
            label_x=0.65,
            label_size=40)

png

What about really flat areas?

You might really have to tune the water_ntile and lake_flatness to get the water right. You can set them to 0 if you do not want any water marked.

rm = RidgeMap((-71.167374,42.324286,-70.952454, 42.402672))
values = rm.get_elevation_data(num_lines=50)
rm.plot_map(values=rm.preprocess(values=values, lake_flatness=4, water_ntile=30, vertical_ratio=20),
            label='Cambridge\nand Boston',
            label_x=0.75,
            label_size=40,
            linewidth=1)

png

What about Walden Pond?

It is that pleasant kettle pond in the bottom right of this map, looking entirely comfortable with its place in Western writing and thought.

rm = RidgeMap((-71.418858,42.427511,-71.310024,42.481719))
values = rm.get_elevation_data(num_lines=100)
rm.plot_map(values=rm.preprocess(values=values, water_ntile=15, vertical_ratio=30),
            label='Concord\nMassachusetts',
            label_x=0.1,
            label_size=30)

png

Do you play nicely with other matplotlib figures?

Of course! If you really want to put a stylized elevation map in a scientific plot you are making, I am not going to stop you, and will actually make it easier for you. Just pass an argument for ax to RidgeMap.plot_map.

import numpy as np
fig, axes = plt.subplots(ncols=2, figsize=(20, 5))
x = np.linspace(-2, 2)
y = x * x

axes[0].plot(x, y, 'o')

rm = RidgeMap()
rm.plot_map(label_size=24, background_color=(1, 1, 1), ax=axes[1])

png

User Examples

Annotating, changing background color, custom text

This example shows how to annotate a lat/long on the map, and updates the color of the label text to allow for a dark background. Thanks to kratsg for contributing.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

bgcolor = np.array([65,74,76])/255.

scipp = (-122.060510, 36.998776)
rm = RidgeMap((-122.087116,36.945365,-121.999226,37.023250))
scipp_coords = ((scipp[0] - rm.longs[0])/(rm.longs[1] - rm.longs[0]),(scipp[1] - rm.lats[0])/(rm.lats[1] - rm.lats[0]))

values = rm.get_elevation_data(num_lines=150)
ridges = rm.plot_map(values=rm.preprocess(values=values,
                                          lake_flatness=1,
                                          water_ntile=0,
                                          vertical_ratio=240),
            label='Santa Cruz\nMountains',
            label_x=0.75,
            label_y=0.05,
            label_size=36,
            kind='elevation',
            linewidth=1,
            background_color=bgcolor,
            line_color = plt.get_cmap('cool'))

# Bit of a hack to update the text label color
for child in ridges.get_children():
    if isinstance(child, matplotlib.text.Text) and 'Santa Cruz' in child._text:
        label_artist = child
        break
label_artist.set_color('white')

ridges.text(scipp_coords[0]+0.005, scipp_coords[1]+0.005, 'SCIPP',
            fontproperties=rm.font,
            size=20,
            color="white",
            transform=ridges.transAxes,
            verticalalignment="bottom",
            zorder=len(values)+10)

ridges.plot(*scipp_coords, 'o',
            color='white',
            transform=ridges.transAxes,
            ms=6,
            zorder=len(values)+10)

png

Elevation Data

Elevation data used by ridge_map comes from NASA's Shuttle Radar Topography Mission (SRTM), high resolution topographic data collected in 2000, and released in 2015. SRTM data are sampled at a resolution of 1 arc-second (about 30 meters). SRTM data is provided to ridge_map via the python package SRTM.py (link). SRTM data is not available for latitudes greater than N 60° or less than S 60°:

gif

More Repositories

1

imcmc

Image Markov Chain Monte Carlo
Python
237
star
2

minimc

Just a little MCMC
Python
213
star
3

strava_calendar

Visualizations from Strava data in matplotlib
Python
85
star
4

sampled

Decorator for PyMC3
Python
49
star
5

flask_react_example

JavaScript
31
star
6

ppl-api

A comparison of PPL APIs
Jupyter Notebook
24
star
7

quantile_dotplot

Python implementation of plot from Kay, Kola, Hullman, Munson "When (ish) is My Bus?" (2016)
Python
18
star
8

pydata_nyc2017

Slides and materials for workshop on "Two views on regression with PyMC3 and scikit-learn"
Jupyter Notebook
18
star
9

couplings

Unbiased MCMC with couplings
Jupyter Notebook
17
star
10

callisto

A command line utility to create kernels in Jupyter from virtual environments.
Python
16
star
11

hamiltonian_monte_carlo_talk

Essay on Hamiltonian Monte Carlo in PyMC3
Jupyter Notebook
14
star
12

flask_angular_example

a minimal example of a data bound sklearn model
JavaScript
14
star
13

carpo

Run and time jupyter notebooks
Python
12
star
14

working_ml

Examples of applied machine learning
Jupyter Notebook
12
star
15

mcmc-adapt

A poster for Scipy 2021
HTML
10
star
16

skample

Sample data from sketches
JavaScript
7
star
17

redistricting-pymc3-pycon-2018

Code and notebooks for "Fighting Gerrymandering with PyMC3" from PyCon 2018
Jupyter Notebook
6
star
18

ngMathJax

Live rendering with AngularJS and MathJax
HTML
5
star
19

yourplotlib

PyData NYC 2019 talk on building a maintainable plotting library
Jupyter Notebook
5
star
20

compart

An experimental library for doing computational art.
Jupyter Notebook
4
star
21

tidytex

Keep LaTeX folders clean and compile automatically
Python
2
star
22

march_madness_viewer

View March Madness 2015 Picks
JavaScript
2
star
23

newsreader

Test Reddit scraper
Python
2
star
24

l2hmc_pymc3

Standalone implementation of sampler from Levy, Hoffman, Sohl-Dickson's paper, in PyMC3
Python
2
star
25

flymc3

Flask + PyMC3
HTML
2
star
26

adventofcode

My advent of code excursions.
Julia
2
star
27

hmc_tuning_talk

"Pragmatic Probabilistic Programming: Parameter Adaptation in PyMC3" talk from 2019 Probabilistic Programming Summit
JavaScript
2
star
28

driving_fatalities

Case study in state fatality rates using partial pooling with PyMC3
Jupyter Notebook
2
star
29

email_fetcher

Fetches your emails
Python
1
star
30

run_mapper

R code for mapping .gpx files
R
1
star
31

march_madness

2015 Kaggle Competition
Python
1
star
32

arviz_pydata_nyc

Talk on ArviZ at PyData NYC
Jupyter Notebook
1
star
33

golearn

Experiments in machine learning with Go
Go
1
star
34

probprog_poster

ArviZ poster from probprog 2018 (https://probprog.cc), joint with Austin Rochford
TeX
1
star
35

dotfiles

My personal dotfiles
Shell
1
star
36

prob_prog_experiments

Prototypes in probabilistic programming
Python
1
star
37

lin_reg_essay

Essay from talk slides on linear regression
HTML
1
star
38

sde_experiment

An experimenting in simulating time series data.
JavaScript
1
star
39

seen_it_news

Code for a hipster twitter bot
Python
1
star
40

intro_ml_talk

Overview of ML
Jupyter Notebook
1
star
41

pete

He's just happy to be here
Python
1
star
42

bayesian_reg_talk

Slides from talk on bayesian regression
JavaScript
1
star
43

tourney_viewer

Viewer for Kaggle March Madness predictions
JavaScript
1
star
44

blogwork

Scripts from my blog
Python
1
star
45

parametric_graphs

Parametric graphs in d3.js
HTML
1
star
46

ColCarroll.github.io

CSS
1
star
47

jshmc

Interactive HMC
JavaScript
1
star