bar.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 bar(
  8. table: wandb.Table,
  9. label: str,
  10. value: str,
  11. title: str = "",
  12. split_table: bool = False,
  13. ) -> CustomChart:
  14. """Constructs a bar chart from a wandb.Table of data.
  15. Args:
  16. table: A table containing the data for the bar chart.
  17. label: The name of the column to use for the labels of each bar.
  18. value: The name of the column to use for the values of each bar.
  19. title: The title of the bar 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 random
  29. import wandb
  30. # Generate random data for the table
  31. data = [
  32. ["car", random.uniform(0, 1)],
  33. ["bus", random.uniform(0, 1)],
  34. ["road", random.uniform(0, 1)],
  35. ["person", random.uniform(0, 1)],
  36. ]
  37. # Create a table with the data
  38. table = wandb.Table(data=data, columns=["class", "accuracy"])
  39. # Initialize a W&B run and log the bar plot
  40. with wandb.init(project="bar_chart") as run:
  41. # Create a bar plot from the table
  42. bar_plot = wandb.plot.bar(
  43. table=table,
  44. label="class",
  45. value="accuracy",
  46. title="Object Classification Accuracy",
  47. )
  48. # Log the bar chart to W&B
  49. run.log({"bar_plot": bar_plot})
  50. ```
  51. """
  52. return plot_table(
  53. data_table=table,
  54. vega_spec_name="wandb/bar/v0",
  55. fields={"label": label, "value": value},
  56. string_fields={"title": title},
  57. split_table=split_table,
  58. )