histogram.py 1.7 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 histogram(
  8. table: wandb.Table,
  9. value: str,
  10. title: str = "",
  11. split_table: bool = False,
  12. ) -> CustomChart:
  13. """Constructs a histogram chart from a W&B Table.
  14. Args:
  15. table: The W&B Table containing the data for the histogram.
  16. value: The label for the bin axis (x-axis).
  17. title: The title of the histogram plot.
  18. split_table: Whether the table should be split into a separate section
  19. in the W&B UI. If `True`, the table will be displayed in a section named
  20. "Custom Chart Tables". Default is `False`.
  21. Returns:
  22. CustomChart: A custom chart object that can be logged to W&B. To log the
  23. chart, pass it to `wandb.log()`.
  24. Example:
  25. ```python
  26. import math
  27. import random
  28. import wandb
  29. # Generate random data
  30. data = [[i, random.random() + math.sin(i / 10)] for i in range(100)]
  31. # Create a W&B Table
  32. table = wandb.Table(
  33. data=data,
  34. columns=["step", "height"],
  35. )
  36. # Create a histogram plot
  37. histogram = wandb.plot.histogram(
  38. table,
  39. value="height",
  40. title="My Histogram",
  41. )
  42. # Log the histogram plot to W&B
  43. with wandb.init(...) as run:
  44. run.log({"histogram-plot1": histogram})
  45. ```
  46. """
  47. return plot_table(
  48. data_table=table,
  49. vega_spec_name="wandb/histogram/v0",
  50. fields={"value": value},
  51. string_fields={"title": title},
  52. split_table=split_table,
  53. )