# Set global plot defaults for Observable Plot

**URL:** <https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797>\
**Category:** Help\
**Created:** [March 28, 2023, 9:06am UTC](https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797 "2023-03-28T09:06:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erdirstats](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/erdirstats/32/7607_2.png) [@erdirstats](https://talk.observablehq.com/u/erdirstats)\
**Post date:** [March 28, 2023, 9:06am UTC](https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797/1 "2023-03-28T09:06:21Z")

</div>

Hi. I have been using Observable Plot within quarto documents to create interactive plots for HTML output. It really works so well! Is there any way to change the defaults of Plot so they apply to all the plots in a document?  
For example, setting `backgroundColor: "red"` in a way it applies to all plots, without the need to change it in each plot. The same for other plot properties (font, etc).

Thanks!

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [March 28, 2023, 10:59am UTC](https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797/2 "2023-03-28T10:59:50Z")

</div>

This is not possible yet, but you can follow the feature request (and vote it up) here [Create plot themes · Issue #630 · observablehq/plot · GitHub](https://github.com/observablehq/plot/issues/630)

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [March 28, 2023, 5:20pm UTC](https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797/3 "2023-03-28T17:20:47Z")

</div>

One way is to define your theme as an options object like so:

```js
theme = ({
  style: "background-color: black; color: white;"
})

```

Then you pass your theme options in to Plot.plot:

```js
Plot.plot({
  ...theme,
  marks: [
    Plot.barY(alphabet, {x: "letter", y: "frequency"})
  ]
})

```

Or in shorthand:

```js
Plot.barY(alphabet, {x: "letter", y: "frequency"}).plot(theme)

```

Here’s a related tutorial on this technique:

> **[Custom theme elements for reuse in Observable Plot](https://observablehq.com/@observablehq/custom-plot-themes)**
>
> Whether making branded reports for your company or making consistently-styled charts for your personal blog, it can be useful to have custom theme elements on-hand for reuse in your Observable Plot charts. Here, we get you started with your own...

If you like, you could also wrap Plot.plot to apply these default options:

```js
function myplot({
  style = "background-color: black; color: white;",
  ...options
} = {}) {
  return Plot.plot({style, ...options});
}

```

Then you would replace calls to Plot.plot with myplot, like so:

 ![untitled (34)](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/b/bce44ae6038b3d4643ea08e435cbb0af44e3f7d8.png)

```js
myplot({marks: [Plot.barY(alphabet, {x: "letter", y: "frequency"})]})

```

(This approach won’t work with Plot.barY(…).plot(), though.)

If you really want to override the behavior of Plot so that you don’t have to rewrite your code, then you could monkey patch Plot like so:

```js
Plot = {
  const Plot = await require("@observablehq/plot@0.6.4/dist/plot.umd.min.js");
  const {plot} = Plot; // capture the base plot before overriding

  // A themed version of Plot.plot, altering the default options. Note that
  // nested options (e.g., x.grid) will require fancier destructuring.
  function myplot({style = "background-color: black; color: white;", ...options} = {}) {
    return plot({style, ...options});
  }

  // Override Plot.plot to use the theme.
  Plot.plot = myplot;

  // Override Mark.prototoype.plot for mark shorthand.
  Plot.Mark.prototype.plot = function({marks = [], ...options} = {}) {
    return myplot({...options, marks: [...marks, this]});
  };

  return Plot;
}

```

But it’s messy to do this; for example if the code above runs more than once, it will wrap the themed plot to apply the theme twice! S I recommend using a helper plot method instead. As @Fil said we’d like to offer more explicit supporting for theming within Plot soon. Then it’d probably be something like

```js
MyPlot = Plot.theme({style = "background-color: black; color: white;"})

```

and then calling MyPlot.plot or MyPlot.barY instead of Plot.plot and Plot.barY.

---

<div class="post-metadata">

**Author:** ![erdirstats](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/erdirstats/32/7607_2.png) [@erdirstats](https://talk.observablehq.com/u/erdirstats)\
**Post date:** [March 29, 2023, 6:48am UTC](https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797/4 "2023-03-29T06:48:13Z")

</div>

Thanks for the response! The `theme` solution you propose is very nice.
