--- name: data-visualization description: "Data visualization with chart selection, color theory, and annotation best practices. Covers chart types (bar, line, scatter, heatmap), axes rules, and storytelling with data. Use for: charts, graphs, dashboards, reports, presentations, infographics, data stories. Triggers: data visualization, chart, graph, data chart, bar chart, line chart, scatter plot, data viz, visualization, dashboard chart, infographic data, data presentation, chart design, plot, heatmap, pie chart alternative" allowed-tools: Bash(infsh *) --- # Data Visualization Create clear, effective data visualizations via [inference.sh](https://inference.sh) CLI. ## Quick Start ```bash curl -fsSL https://cli.inference.sh | sh && infsh login # Generate a chart with Python infsh app run infsh/python-executor --input '{ "code": "import matplotlib.pyplot as plt\nimport matplotlib\nmatplotlib.use(\"Agg\")\n\nmonths = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\"]\nrevenue = [42, 48, 55, 61, 72, 89]\n\nfig, ax = plt.subplots(figsize=(10, 6))\nax.bar(months, revenue, color=\"#3b82f6\", width=0.6)\nax.set_ylabel(\"Revenue ($K)\")\nax.set_title(\"Monthly Revenue Growth\", fontweight=\"bold\")\nfor i, v in enumerate(revenue):\n ax.text(i, v + 1, f\"${v}K\", ha=\"center\", fontweight=\"bold\")\nplt.tight_layout()\nplt.savefig(\"revenue.png\", dpi=150)\nprint(\"Saved\")" }' ``` ## Chart Selection Guide ### Which Chart for Which Data? | Data Relationship | Best Chart | Never Use | |------------------|-----------|-----------| | **Change over time** | Line chart | Pie chart | | **Comparing categories** | Bar chart (horizontal for many categories) | Line chart | | **Part of a whole** | Stacked bar, treemap | Pie chart (controversial but: bar is always clearer) | | **Distribution** | Histogram, box plot | Bar chart | | **Correlation** | Scatter plot | Bar chart | | **Ranking** | Horizontal bar chart | Vertical bar, pie | | **Geographic** | Choropleth map | Bar chart | | **Composition over time** | Stacked area chart | Multiple pie charts | | **Single metric** | Big number (KPI card) | Any chart (overkill) | | **Flow / process** | Sankey diagram | Bar chart | ### The Pie Chart Problem Pie charts are almost always the wrong choice: ``` ❌ Pie chart problems: - Hard to compare similar-sized slices - Can't show more than 5-6 categories - 3D pie charts are always wrong - Impossible to read exact values ✅ Use instead: - Horizontal bar chart (easy comparison) - Stacked bar (part of whole) - Treemap (hierarchical parts) - Just a table (if precision matters) ``` ## Design Rules ### Axes | Rule | Why | |------|-----| | Always start Y-axis at 0 (bar charts) | Prevents misleading visual | | Line charts CAN start above 0 | When showing change, not absolute values | | Label both axes | Reader shouldn't have to guess units | | Remove unnecessary gridlines | Reduce visual noise | | Use horizontal labels | Vertical text is hard to read | | Sort bar charts by value | Don't use alphabetical order unless there's a reason | ### Color | Principle | Application | |-----------|------------| | **Max 5-7 colors** per chart | More becomes unreadable | | **Highlight one thing** | Grey everything else, color the focus | | **Sequential** for magnitude | Light → dark for low → high | | **Diverging** for positive/negative | Red ← neutral → blue | | **Categorical** for groups | Distinct hues, similar brightness | | **Colorblind-safe** | Avoid red/green only — add shapes or labels | | **Consistent meaning** | If blue = revenue, keep it blue everywhere | ### Good Color Palettes ```python # Sequential (low to high) sequential = ["#eff6ff", "#bfdbfe", "#60a5fa", "#2563eb", "#1d4ed8"] # Diverging (negative to positive) diverging = ["#ef4444", "#f87171", "#d1d5db", "#34d399", "#10b981"] # Categorical (distinct groups) categorical = ["#3b82f6", "#f59e0b", "#10b981", "#8b5cf6", "#ef4444"] # Colorblind-safe cb_safe = ["#0077BB", "#33BBEE", "#009988", "#EE7733", "#CC3311"] ``` ### Text and Labels | Element | Rule | |---------|------| | **Title** | States the insight, not the data type. "Revenue doubled in Q2" not "Q2 Revenue Chart" | | **Annotations** | Call out key data points directly on the chart | | **Legend** | Avoid if possible — label directly on chart lines/bars | | **Font size** | Minimum 12px, 14px+ for presentations | | **Number format** | Use K, M, B for large numbers (42K not 42,000) | | **Data labels** | Add to bars/points when exact values matter | ## Chart Recipes ### Line Chart (Time Series) ```bash infsh app run infsh/python-executor --input '{ "code": "import matplotlib.pyplot as plt\nimport matplotlib\nmatplotlib.use(\"Agg\")\n\nfig, ax = plt.subplots(figsize=(12, 6))\nfig.patch.set_facecolor(\"white\")\n\nmonths = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\nthis_year = [120, 135, 148, 162, 178, 195, 210, 228, 245, 268, 290, 320]\nlast_year = [95, 102, 108, 115, 122, 130, 138, 145, 155, 165, 178, 190]\n\nax.plot(months, this_year, color=\"#3b82f6\", linewidth=2.5, marker=\"o\", markersize=6, label=\"2024\")\nax.plot(months, last_year, color=\"#94a3b8\", linewidth=2, linestyle=\"--\", label=\"2023\")\nax.fill_between(range(len(months)), last_year, this_year, alpha=0.1, color=\"#3b82f6\")\n\nax.annotate(\"$320K\", xy=(11, 320), fontsize=14, fontweight=\"bold\", color=\"#3b82f6\")\nax.annotate(\"$190K\", xy=(11, 190), fontsize=12, color=\"#94a3b8\")\n\nax.set_ylabel(\"Revenue ($K)\", fontsize=12)\nax.set_title(\"Revenue grew 68% year-over-year\", fontsize=16, fontweight=\"bold\")\nax.legend(fontsize=12)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.grid(axis=\"y\", alpha=0.3)\nplt.tight_layout()\nplt.savefig(\"line-chart.png\", dpi=150)\nprint(\"Saved\")" }' ``` ### Horizontal Bar Chart (Comparison) ```bash infsh app run infsh/python-executor --input '{ "code": "import matplotlib.pyplot as plt\nimport matplotlib\nmatplotlib.use(\"Agg\")\n\nfig, ax = plt.subplots(figsize=(10, 6))\n\ncategories = [\"Email\", \"Social\", \"SEO\", \"Paid Ads\", \"Referral\", \"Direct\"]\nvalues = [12, 18, 35, 22, 8, 5]\ncolors = [\"#94a3b8\"] * len(values)\ncolors[2] = \"#3b82f6\" # Highlight the winner\n\n# Sort by value\nsorted_pairs = sorted(zip(values, categories, colors))\nvalues, categories, colors = zip(*sorted_pairs)\n\nax.barh(categories, values, color=colors, height=0.6)\nfor i, v in enumerate(values):\n ax.text(v + 0.5, i, f\"{v}%\", va=\"center\", fontsize=12, fontweight=\"bold\")\n\nax.set_xlabel(\"% of Total Traffic\", fontsize=12)\nax.set_title(\"SEO drives the most traffic\", fontsize=16, fontweight=\"bold\")\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nplt.tight_layout()\nplt.savefig(\"bar-chart.png\", dpi=150)\nprint(\"Saved\")" }' ``` ### KPI / Big Number Card ```bash infsh app run infsh/html-to-image --input '{ "html": "
Monthly Revenue
$89K
↑ 23% vs last month
Active Users
12.4K
↑ 8% vs last month
Churn Rate
2.1%
↑ 0.3% vs last month