Render a graph of percentages.

This commit is contained in:
Julien Palard 2021-12-08 00:24:09 +01:00
parent 558aba63e2
commit 8aa82b4837
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
5 changed files with 54 additions and 1 deletions

View File

@ -20,5 +20,11 @@ Then plot it using:
## Data ## Data
### Number of pypi.org downloads
![](python-versions.png) ![](python-versions.png)
### Percentage of pypi.org downloads
![](python-versions-pct.png)

18
cy.py Normal file
View File

@ -0,0 +1,18 @@
from cycler import cycler
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 50)
offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False)
yy = np.transpose([np.sin(x + phi) for phi in offsets])
plt.rc("axes", prop_cycle=cycler(linestyle=["-", "--", ":", "-."]))
# fig, (ax0, ax1) = plt.subplots(nrows=2)
plt.plot(yy)
# ax0.set_title("Set default color cycle to rgby")
# Add a bit more space between the two plots.
# fig.subplots_adjust(hspace=0.3)
plt.show()

BIN
python-versions-pct.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -6,7 +6,9 @@ import sqlite3
import sys import sys
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date
from collections import defaultdict from collections import defaultdict
from itertools import cycle
import pandas as pd
from pypinfo.fields import PythonVersion from pypinfo.fields import PythonVersion
from pypinfo.core import build_query, create_client, create_config, parse_query_result from pypinfo.core import build_query, create_client, create_config, parse_query_result
from pypinfo.db import get_credentials from pypinfo.db import get_credentials
@ -118,6 +120,7 @@ def plot_main():
by_version[row["python_version"]][1].append(row["download_count"]) by_version[row["python_version"]][1].append(row["download_count"])
plt.style.use("tableau-colorblind10") plt.style.use("tableau-colorblind10")
plt.figure(figsize=(10, 10 * 2 / 3)) plt.figure(figsize=(10, 10 * 2 / 3))
fmt = iter(cycle(["-", "--", ":", "-."]))
for version, (x, y) in by_version.items(): for version, (x, y) in by_version.items():
if version is None: if version is None:
continue continue
@ -127,14 +130,40 @@ def plot_main():
smooth_x = np.linspace(date2num(min(x)), date2num(max(x)), 200) smooth_x = np.linspace(date2num(min(x)), date2num(max(x)), 200)
spline = make_interp_spline([date2num(d) for d in x], y, k=2) spline = make_interp_spline([date2num(d) for d in x], y, k=2)
smooth_y = spline(smooth_x) smooth_y = spline(smooth_x)
plt.plot_date(smooth_x, smooth_y, label=version, fmt="-") plt.plot_date(smooth_x, smooth_y, label=version, fmt=next(fmt))
plt.xlabel("Date") plt.xlabel("Date")
plt.ylabel("PyPI downloads") plt.ylabel("PyPI downloads")
plt.legend() plt.legend()
plt.savefig("python-versions.png") plt.savefig("python-versions.png")
def plot_pct():
db = DB()
versions = pd.DataFrame(
db.fetch_python_version(),
columns=["start_date", "end_date", "python_version", "download_count"],
dtype="str",
)
versions["download_count"] = pd.to_numeric(versions["download_count"])
versions["python_version"].fillna("Other", inplace=True)
versions = versions.merge(
versions.groupby("start_date").agg(monthly_downloads=("download_count", "sum")),
on="start_date",
)
versions["pct"] = 100 * versions.download_count / versions.monthly_downloads
versions["date"] = pd.to_datetime(versions.start_date) + timedelta(days=14)
versions.set_index(["python_version", "date"], inplace=True)
to_plot = versions.pct.unstack(0, fill_value=0)
to_plot["Other"] += to_plot["2.6"] + to_plot["3.3"] + to_plot["3.4"]
del to_plot["2.6"], to_plot["3.3"], to_plot["3.4"]
print(to_plot)
plt.style.use("tableau-colorblind10")
to_plot.plot.area(stacked=True, figsize=(10, 10 * 2 / 3))
plt.savefig("python-versions-pct.png")
if __name__ == "__main__": if __name__ == "__main__":
if "--fetch" in sys.argv: if "--fetch" in sys.argv:
fetch_main() fetch_main()
plot_pct()
plot_main() plot_main()