|
| 1 | +import matplotlib.pyplot as plt # type: ignore |
| 2 | +import matplotlib # type: ignore |
| 3 | +from mpl_toolkits.axes_grid1 import make_axes_locatable # type: ignore |
| 4 | +import geopandas as gpd # type: ignore |
| 5 | +from typing import Literal, Dict |
| 6 | + |
| 7 | + |
| 8 | +def make_chart( |
| 9 | + chart_data: Dict, |
| 10 | + chart_type: Literal["bar", "pie", "line", "scatter", "hist", "box", "area"] = "bar", |
| 11 | + data_label: str = None, |
| 12 | + **kwargs, |
| 13 | +) -> plt.Figure: |
| 14 | + matplotlib.rcParams["font.size"] = 15 |
| 15 | + matplotlib.rcParams["axes.labelcolor"] = "Black" |
| 16 | + |
| 17 | + if chart_type == "bar": |
| 18 | + plt.bar(chart_data.keys(), chart_data.values(), **kwargs) |
| 19 | + if data_label in [ |
| 20 | + "last_n_days_analysis", |
| 21 | + "clicks_analysis", |
| 22 | + "unique_clicks_analysis", |
| 23 | + ]: |
| 24 | + plt.xticks(rotation=90) |
| 25 | + elif chart_type == "pie": |
| 26 | + plt.pie(chart_data.values(), labels=chart_data.keys(), **kwargs) |
| 27 | + elif chart_type == "line": |
| 28 | + plt.plot(chart_data.keys(), chart_data.values(), **kwargs) |
| 29 | + elif chart_type == "scatter": |
| 30 | + plt.scatter(chart_data.keys(), chart_data.values(), **kwargs) |
| 31 | + elif chart_type == "hist": |
| 32 | + plt.hist(list(chart_data.values()), **kwargs) |
| 33 | + elif chart_type == "box": |
| 34 | + plt.boxplot(list(chart_data.values()), **kwargs) |
| 35 | + elif chart_type == "area": |
| 36 | + plt.stackplot(chart_data.keys(), chart_data.values(), **kwargs) |
| 37 | + else: |
| 38 | + raise Exception( |
| 39 | + "Invalid chart type. Valid chart types are: bar, pie, line, scatter, hist, box, area" |
| 40 | + ) |
| 41 | + return plt |
| 42 | + |
| 43 | + |
| 44 | +def _create_heatmap( |
| 45 | + data_analysis: Dict[str, int], |
| 46 | + title: str, |
| 47 | + merge_column: str = "NAME", |
| 48 | + cmap: Literal["YlOrRd", "viridis", "plasma", "inferno", "RdPu_r"] = "YlOrRd", |
| 49 | +) -> plt.Figure: |
| 50 | + matplotlib.rcParams["font.size"] = 15 |
| 51 | + matplotlib.rcParams["axes.labelcolor"] = "White" |
| 52 | + world = gpd.read_file("py_spoo_url/data/ne_110m_admin_0_countries.zip") |
| 53 | + world = world.merge( |
| 54 | + gpd.GeoDataFrame(data_analysis.items(), columns=["Country", "Value"]), |
| 55 | + how="left", |
| 56 | + left_on=merge_column, |
| 57 | + right_on="Country", |
| 58 | + ) |
| 59 | + fig, ax = plt.subplots( |
| 60 | + 1, 1, figsize=(15, 10), facecolor=(32 / 255, 34 / 255, 37 / 255, 0.5) |
| 61 | + ) |
| 62 | + plt.subplots_adjust(left=0.05, right=0.90, bottom=0.05, top=0.95) |
| 63 | + for spine in ax.spines.values(): |
| 64 | + spine.set_color((46 / 255, 48 / 255, 53 / 255)) |
| 65 | + spine.set_linewidth(2) |
| 66 | + ax.tick_params(labelcolor="white") |
| 67 | + world.boundary.plot(ax=ax, linewidth=1) |
| 68 | + divider = make_axes_locatable(ax) |
| 69 | + cax = divider.append_axes("right", size="5%", pad=0.1) |
| 70 | + p = world.plot( |
| 71 | + column="Value", |
| 72 | + ax=ax, |
| 73 | + legend=True, |
| 74 | + cax=cax, |
| 75 | + cmap=cmap, |
| 76 | + edgecolor=None, |
| 77 | + legend_kwds={"label": "Clicks"}, |
| 78 | + alpha=0.9, |
| 79 | + ) |
| 80 | + p.set_facecolor((32 / 255, 34 / 255, 37 / 255, 0.5)) |
| 81 | + cbax = cax |
| 82 | + cbax.tick_params(labelcolor="white") |
| 83 | + plt.suptitle(title, x=0.5, y=0.95, fontsize=20, fontweight=3, color="white") |
| 84 | + return plt |
| 85 | + |
| 86 | + |
| 87 | +def make_countries_heatmap( |
| 88 | + country_analysis: Dict[str, int], |
| 89 | + cmap: Literal["YlOrRd", "viridis", "plasma", "inferno", "RdPu_r"] = "YlOrRd", |
| 90 | +) -> plt.Figure: |
| 91 | + return _create_heatmap( |
| 92 | + data_analysis=country_analysis, |
| 93 | + title="Countries Heatmap", |
| 94 | + merge_column="NAME", |
| 95 | + cmap=cmap, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def make_unique_countries_heatmap( |
| 100 | + unique_country_analysis: Dict[str, int], |
| 101 | + cmap: Literal["YlOrRd", "viridis", "plasma", "inferno", "RdPu_r"] = "YlOrRd", |
| 102 | +) -> plt.Figure: |
| 103 | + return _create_heatmap( |
| 104 | + data_analysis=unique_country_analysis, |
| 105 | + title="Unique Countries Heatmap", |
| 106 | + merge_column="NAME", |
| 107 | + cmap=cmap, |
| 108 | + ) |
0 commit comments