scatter.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 scatter(
  8. table: wandb.Table,
  9. x: str,
  10. y: str,
  11. title: str = "",
  12. split_table: bool = False,
  13. ) -> CustomChart:
  14. """Constructs a scatter plot from a wandb.Table of data.
  15. Args:
  16. table: The W&B Table containing the data to visualize.
  17. x: The name of the column used for the x-axis.
  18. y: The name of the column used for the y-axis.
  19. title: The title of the scatter chart.
  20. split_table: Whether the table should be split into a separate section
  21. in the W&B UI. If `True`, the table will be displayed in a section named
  22. "Custom Chart Tables". Default is `False`.
  23. Returns:
  24. CustomChart: A custom chart object that can be logged to W&B. To log the
  25. chart, pass it to `wandb.log()`.
  26. Example:
  27. ```python
  28. import math
  29. import random
  30. import wandb
  31. # Simulate temperature variations at different altitudes over time
  32. data = [
  33. [i, random.uniform(-10, 20) - 0.005 * i + 5 * math.sin(i / 50)]
  34. for i in range(300)
  35. ]
  36. # Create W&B table with altitude (m) and temperature (°C) columns
  37. table = wandb.Table(data=data, columns=["altitude (m)", "temperature (°C)"])
  38. # Initialize W&B run and log the scatter plot
  39. with wandb.init(project="temperature-altitude-scatter") as run:
  40. # Create and log the scatter plot
  41. scatter_plot = wandb.plot.scatter(
  42. table=table,
  43. x="altitude (m)",
  44. y="temperature (°C)",
  45. title="Altitude vs Temperature",
  46. )
  47. run.log({"altitude-temperature-scatter": scatter_plot})
  48. ```
  49. """
  50. return plot_table(
  51. data_table=table,
  52. vega_spec_name="wandb/scatter/v0",
  53. fields={"x": x, "y": y},
  54. string_fields={"title": title},
  55. split_table=split_table,
  56. )