line.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING
  3. from wandb.plot.custom_chart import plot_table
  4. if TYPE_CHECKING:
  5. import wandb
  6. from wandb.plot.custom_chart import CustomChart
  7. def line(
  8. table: wandb.Table,
  9. x: str,
  10. y: str,
  11. stroke: str | None = None,
  12. title: str = "",
  13. split_table: bool = False,
  14. ) -> CustomChart:
  15. """Constructs a customizable line chart.
  16. Args:
  17. table: The table containing data for the chart.
  18. x: Column name for the x-axis values.
  19. y: Column name for the y-axis values.
  20. stroke: Column name to differentiate line strokes (e.g., for
  21. grouping lines).
  22. title: Title of the chart.
  23. split_table: Whether the table should be split into a separate section
  24. in the W&B UI. If `True`, the table will be displayed in a section named
  25. "Custom Chart Tables". Default is `False`.
  26. Returns:
  27. CustomChart: A custom chart object that can be logged to W&B. To log the
  28. chart, pass it to `wandb.log()`.
  29. Example:
  30. ```python
  31. import math
  32. import random
  33. import wandb
  34. # Create multiple series of data with different patterns
  35. data = []
  36. for i in range(100):
  37. # Series 1: Sinusoidal pattern with random noise
  38. data.append([i, math.sin(i / 10) + random.uniform(-0.1, 0.1), "series_1"])
  39. # Series 2: Cosine pattern with random noise
  40. data.append([i, math.cos(i / 10) + random.uniform(-0.1, 0.1), "series_2"])
  41. # Series 3: Linear increase with random noise
  42. data.append([i, i / 10 + random.uniform(-0.5, 0.5), "series_3"])
  43. # Define the columns for the table
  44. table = wandb.Table(data=data, columns=["step", "value", "series"])
  45. # Initialize wandb run and log the line chart
  46. with wandb.init(project="line_chart_example") as run:
  47. line_chart = wandb.plot.line(
  48. table=table,
  49. x="step",
  50. y="value",
  51. stroke="series", # Group by the "series" column
  52. title="Multi-Series Line Plot",
  53. )
  54. run.log({"line-chart": line_chart})
  55. ```
  56. """
  57. return plot_table(
  58. data_table=table,
  59. vega_spec_name="wandb/line/v0",
  60. fields={"x": x, "y": y, "stroke": stroke},
  61. string_fields={"title": title},
  62. split_table=split_table,
  63. )