Skip to content

Instantly share code, notes, and snippets.

@databill86
Forked from gVallverdu/capp_treemaps.py
Created August 1, 2019 07:11
Show Gist options
  • Save databill86/d34251f5bf3b4d93721fdd075a01b94b to your computer and use it in GitHub Desktop.
Save databill86/d34251f5bf3b4d93721fdd075a01b94b to your computer and use it in GitHub Desktop.

Revisions

  1. @gVallverdu gVallverdu revised this gist Jul 20, 2016. No changes.
  2. @gVallverdu gVallverdu revised this gist Jul 20, 2016. 1 changed file with 25 additions and 15 deletions.
    40 changes: 25 additions & 15 deletions capp_treemaps.py
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,16 @@
    import pandas as pd
    import squarify

    # qualtities plotted
    # squarre area is the town surface area (superf)
    # color scale is the town population in 2011 (p11_pop)

    # read data from csv file
    # data from CAPP opendata http://opendata.agglo-pau.fr/index.php/fiche?idQ=27
    df = pd.read_csv("Evolution_et_structure_de_la_population/Evolution_structure_population.csv", sep=";")
    df = df.set_index("libgeo")
    df = df[["superf", "p11_pop"]]
    df2 = df.sort_values(by="superf", ascending=False)
    df3 = df2.drop("PAU")

    # qualtities plotted
    # squarre area is the town surface area (superf)
    # color scale is the town population in 2011 (p11_pop)

    # treemap parameters
    x = 0.
    @@ -23,28 +22,39 @@
    cmap = matplotlib.cm.viridis

    # color scale on the population
    cvalues = df3.p11_pop
    norm = matplotlib.colors.Normalize(vmin=cvalues.min(), vmax=cvalues.max())
    colors = [cmap(norm(value)) for value in cvalues]
    # min and max values without Pau
    mini, maxi = df2.drop("PAU").p11_pop.min(), df2.drop("PAU").p11_pop.max()
    norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
    colors = [cmap(norm(value)) for value in df2.p11_pop]
    colors[1] = "#FBFCFE"

    # labels for squares
    labels = ["%s\nS = %d km2\npop = %d" % (label) for label in zip(df3.index, df3.superf, df3.p11_pop)]
    labels = ["%s\n%d km2\n%d hab" % (label) for label in zip(df2.index, df2.superf, df2.p11_pop)]
    labels[11] = "MAZERES-\nLEZONS\n%d km2\n%d hab" % (df2["superf"]["MAZERES-LEZONS"], df2["p11_pop"]["MAZERES-LEZONS"])

    # make plot
    fig = plt.figure(figsize=(12, 10))
    fig.suptitle("Population et superficie dans la CAPP", fontsize=20)
    fig.suptitle("Population et superficie des communes de la CAPP", fontsize=20)
    ax = fig.add_subplot(111, aspect="equal")
    ax = squarify.plot(df3.superf, color=colors, label=labels, ax=ax, alpha=.7)
    ax = squarify.plot(df2.superf, color=colors, label=labels, ax=ax, alpha=.7)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_title("Pau: superficie = %d km2, population = %d\n" % (df2["superf"]["PAU"], df2["p11_pop"]["PAU"]), fontsize=14)
    ax.set_title("L'aire de chaque carré est proportionnelle à la superficie de la commune\n", fontsize=14)

    # color bar
    # color bar
    # create dummy invisible image with a color map
    img = plt.imshow([cvalues], cmap=cmap)
    img = plt.imshow([df2.p11_pop], cmap=cmap)
    img.set_visible(False)
    fig.colorbar(img, orientation="vertical", shrink=.96)

    fig.text(.76, .9, "Population", fontsize=12)
    fig.text(.76, .9, "Population", fontsize=14)
    fig.text(.5, 0.1,
    "Superficie totale %d km2, Population de la CAPP : %d hab" % (df2.superf.sum(), df2.p11_pop.sum()),
    fontsize=14,
    ha="center")
    fig.text(.5, 0.07,
    "Source : http://opendata.agglo-pau.fr/",
    fontsize=14,
    ha="center")

    plt.show()
  3. @gVallverdu gVallverdu created this gist Jul 18, 2016.
    50 changes: 50 additions & 0 deletions capp_treemaps.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    import matplotlib
    import matplotlib.pyplot as plt
    import pandas as pd
    import squarify

    # read data from csv file
    # data from CAPP opendata http://opendata.agglo-pau.fr/index.php/fiche?idQ=27
    df = pd.read_csv("Evolution_et_structure_de_la_population/Evolution_structure_population.csv", sep=";")
    df = df.set_index("libgeo")
    df = df[["superf", "p11_pop"]]
    df2 = df.sort_values(by="superf", ascending=False)
    df3 = df2.drop("PAU")

    # qualtities plotted
    # squarre area is the town surface area (superf)
    # color scale is the town population in 2011 (p11_pop)

    # treemap parameters
    x = 0.
    y = 0.
    width = 100.
    height = 100.
    cmap = matplotlib.cm.viridis

    # color scale on the population
    cvalues = df3.p11_pop
    norm = matplotlib.colors.Normalize(vmin=cvalues.min(), vmax=cvalues.max())
    colors = [cmap(norm(value)) for value in cvalues]

    # labels for squares
    labels = ["%s\nS = %d km2\npop = %d" % (label) for label in zip(df3.index, df3.superf, df3.p11_pop)]

    # make plot
    fig = plt.figure(figsize=(12, 10))
    fig.suptitle("Population et superficie dans la CAPP", fontsize=20)
    ax = fig.add_subplot(111, aspect="equal")
    ax = squarify.plot(df3.superf, color=colors, label=labels, ax=ax, alpha=.7)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_title("Pau: superficie = %d km2, population = %d\n" % (df2["superf"]["PAU"], df2["p11_pop"]["PAU"]), fontsize=14)

    # color bar
    # create dummy invisible image with a color map
    img = plt.imshow([cvalues], cmap=cmap)
    img.set_visible(False)
    fig.colorbar(img, orientation="vertical", shrink=.96)

    fig.text(.76, .9, "Population", fontsize=12)

    plt.show()