Quick Start¶
Welcome! This is a complete, copy-pasteable example to take you from synthetic data generation to a final, publication-quality plot in just a few minutes. We’ll show you how to do it with both the Python API and the Command-Line Interface (CLI).
Part 1: The Python API¶
The most flexible way to use k-diagram is within a Python script
or notebook. The code below will generate a complete, illustrative
example.
Code¶
1import kdiagram as kd
2import matplotlib.pyplot as plt
3
4# 1. Define distinct profiles for the models we want to compare.
5# This ensures they have different performance characteristics, which
6# will make our comparison plot much more interesting.
7model_profiles = {
8 "Baseline": {"bias": -8.0, "noise_std": 3.0},
9 "Upgraded": {"bias": 0.5, "noise_std": 4.0},
10}
11
12# 2. Generate the dataset using these specific profiles.
13print("Generating synthetic data for two models...")
14data = kd.datasets.make_regression_data(
15 model_profiles=model_profiles,
16 seed=42,
17 as_frame=True # We ask for a pandas DataFrame as output
18)
19
20# 3. Save the data so we can use it with the CLI in Part 2
21data.to_csv("quickstart_data.csv", index=False)
22print("Sample data created and saved to quickstart_data.csv")
23
24# 4. Create the comparison plot and display it.
25print("\nGenerating comparison plot from Python API...")
26ax = kd.plot_regression_performance(
27 # Pass the true values and each prediction series as NumPy arrays
28 data['y_true'].values,
29 data['pred_Baseline'].values,
30 data['pred_Upgraded'].values,
31
32 names=["Baseline", "Upgraded"],
33 title="Model Comparison: Baseline vs. Upgraded",
34 # Use metric_labels for a cleaner plot
35 metric_labels={
36 'r2': 'R²',
37 'neg_mean_absolute_error': 'MAE',
38 'neg_root_mean_squared_error': 'RMSE',
39 },
40)
41
42plt.show()
Expected Output¶
Running this script will print a confirmation message and then display a polar radar chart similar to this one, clearly showing the performance difference between the two models.
Part 2: The Command-Line (CLI) Alternative¶
Prefer the command line for quick tasks? You can create the exact same
plot without writing any Python. Since we already saved our data to
quickstart_data.csv, just run this command in your terminal:
k-diagram plot-regression-performance quickstart_data.csv \
--y-true y_true \
--pred pred_Baseline pred_Upgraded \
--names "Baseline" "Upgraded" \
--title "Model Comparison (from CLI)" \
--metric-label "r2:R²" "neg_mean_absolute_error:MAE" \
"neg_root_mean_squared_error:RMSE" \
--savefig quickstart_cli_plot.png
This will save the plot directly to a file named
quickstart_cli_plot.png.
Interpreting the Plot¶
This radar chart provides a rich, holistic view of model performance. Here’s how to read it.
Putting It All Together: Our Analysis¶
The plot tells a very clear story: the “Upgraded” model is a significant improvement over the “Baseline”.
The Upgraded model (green) has long bars on all three axes, forming a large, balanced shape. This indicates it is a strong all-around performer with a high R² (good fit) and low MAE and RMSE (low errors).
The Baseline model (blue) reveals a critical flaw. While its MAE bar is not zero, its bars for R² and RMSE are extremely short. This is a classic sign of a model with a severe, systematic bias. The large, consistent errors are heavily penalized by the R² and RMSE metrics, indicating the model has poor predictive power despite its seemingly acceptable average error.
In conclusion, the chart instantly shows us that the “Upgraded” model successfully fixed the critical bias issue present in the “Baseline”, making it the clear winner.
This is just one example of what this chart can reveal. To see more advanced use cases, including how to add custom metrics or control the normalization, check out the detailed examples in our gallery.
See more examples in Polar Performance Chart
From Synthetic to Real-World Data¶
This quick start guide uses a synthetic dataset to demonstrate the plotting workflow in a simple and reproducible way.
However, the true power of k-diagram shines when analyzing the
complex uncertainty of real-world forecasts. To see these same diagnostic
charts applied to an environmental forecasting challenge, we highly
recommend exploring our detailed case study on land subsidence.
See the full analysis in Case Study: Zhongshan Land Subsidence Uncertainty
Next Steps¶
Congratulations! You’ve created your first k-diagram plot and seen how easy it is to compare models.
Explore more plot types and their capabilities in the Plot Gallery.
Learn about the concepts behind the visualizations in the User Guide.
Refer to the API Reference for detailed function signatures and parameters.