# Plotting an array value

**URL:** <https://community.wandb.ai/t/plotting-an-array-value/4594>\
**Category:** W&B Help\
**Tags:** dashboard\
**Created:** [June 18, 2023, 3:14pm UTC](https://community.wandb.ai/t/plotting-an-array-value/4594 "2023-06-18T15:14:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nialda](https://avatars.discourse-cdn.com/v4/letter/n/258eb7/32.png) [@nialda](https://community.wandb.ai/u/nialda)\
**Post date:** [June 18, 2023, 3:14pm UTC](https://community.wandb.ai/t/plotting-an-array-value/4594/1 "2023-06-18T15:14:13Z")

</div>

I’ve used the following code to log a metric

`wandb.log({"metric": [4, 5, 6]})`

but then found out that Wandb doesn’t support plotting a list by default. I want to create a line plot where the y-axis is the first element of the metric array, and the x-axis is the step.

I’ve read the [Custom Charts](https://docs.wandb.ai/guides/app/features/custom-charts) section of the document. I think I should use Vega to access the first element of the array. Here are the steps that I’ve taken:  
For the custom chart, I’ve set the data source as “history” and selected the “metric” key.

```auto
query {
    runSets
         (runSets: "${runSets}" ) {
            id
            name
            history
                (keys: ["metric"] )
        }
}

```

In the Vega script, I tried to flatten the array, using [this part](https://vega.github.io/vega/docs/transforms/flatten/) of the documentation

```auto
"transform": {
    ...
    {"type": "flatten", "fields": ["${field:metric}"]},
}

```

This gives me a warning that “type” and “fields” arguments are not allowed, which means I should include this flattening block somewhere else (not in the `transform` section).  
I’m afraid I don’t know where, and how I can achieve this.  
Is this even possible?  
If not, I think in my notebook I should write a script that accesses the `wandb.run` log data, and transform the data for each run. if so, any tips for that solution are also appreciated.

I’ve also read similar posts in the community, but their use cases were different.

[cross-posted in Stackoverflow, but since I’m a new user, I can’t put its link here (2 URL limit))

---

<div class="post-metadata">

**Author:** ![nialda](https://avatars.discourse-cdn.com/v4/letter/n/258eb7/32.png) [@nialda](https://community.wandb.ai/u/nialda)\
**Post date:** [June 18, 2023, 4:53pm UTC](https://community.wandb.ai/t/plotting-an-array-value/4594/2 "2023-06-18T16:53:03Z")

</div>

I’ve used this code snippet to add the plot for the previous runs. I thought it might help somebody. but still, if there’s a way to do this with Vega, it would be so much better. as this alternate method requires the following code snippet to be run every time we want to see the updated plots.

```auto
import matplotlib.pyplot as plt
import wandb

runs = api.runs('workspace/project_name')

for i in range(len(runs))[:1]:
    run_history = runs[i].scan_history(keys=["metric"])
    values = [row['metric'] for row in run_history]

    plt.plot(values)
    plt.ylabel("metric")
    wandb.init(
        project="project_name",
        id=run_history.run.id,
        resume=True
    )
    wandb.log({"metric_plot": plt})

```

---

<div class="post-metadata">

**Author:** ![raphael-sanandres](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@raphael-sanandres](https://community.wandb.ai/u/raphael-sanandres)\
**Post date:** [June 22, 2023, 9:52pm UTC](https://community.wandb.ai/t/plotting-an-array-value/4594/3 "2023-06-22T21:52:10Z")

</div>

Hello @nialda !

I believe an easier way would be to use our Weave Table. In order for this to work, you must opt into Weave 1.0 (as shown in the following screenshot.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/2X/a/aa2fcb8c444d2e5915b4af4eea81ff0ad0962e08.png)

[Here](https://wandb.ai/raphael-sanandres/list-log/runs/8wxt8etu?workspace=user-raphael-sanandres) is a workspace where I logged 100 steps of `wandb.log({"metric": [random.randint(0,100), 5, 6]})` in one run and graphed the x-axis as `step` and the y-axis as the first item in the list.

Here are the steps to creating the Table:

1. Press Add Panel → Weave
2. For the query, write in `runs.history.concat`. This will make a table of log history.
3. Go to the bottom right of the panel and select `Columns` and make sure that only `_step` and `metric` is chosen
4. Press the Gear in the top right of the Panel. This should open up your settings. For the X Dim, write in `row["_step"]` and for the Y Dim write in `row["metric"][0]`. This will make a scatterplot. In order to change this to a line plot, change the `Mark` into a `line`.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/2X/0/09d6e3df9f52a43dab57d0e384ab400d7f726d2e.png)

Your resulting Line Graph should look like the image above. I have included the Weave Panel Settings for ease.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [August 21, 2023, 9:53pm UTC](https://community.wandb.ai/t/plotting-an-array-value/4594/4 "2023-08-21T21:53:04Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
