- Home /

A very common thing that we do is to display multiple figures (matplotlib) per cell (for example, within a for loop).

How ever it looks like the display function over writes any pervious figures.

I found this similar question: https://forums.databricks.com/questions/1129/is-there-a-way-to-graph-multiple-line-graphs-from.html

But the answer doesn't quite make sense.

Comment

**Answer** by joshuacook
·
May 09, 2019 at 06:33 PM

`import numpy as np`

`import matplotlib.pyplot as plt`

`# something to plot`

`points = np.array([(-2.9,4.5),`

` (-2.1,2.0),`

` (1.8,-2.7),`

` (0.5,-1.7),`

` (-1.1,1.9)])`

`X = np.array([(1, point[0]) for point in points])`

`x = np.array([point[0] for point in points])`

`y = np.array([point[1] for point in points])`

`XTX = X.T.dot(X)`

`XTy = X.T.dot(y) `

`weights = np.linalg.inv(XTX).dot(XTy)`

`f = lambda x: weights[0] + weights[1]*x`

`functions = [`

` f,`

` lambda x: -.25 -2*x,`

` lambda x: .2 - 1.3*x`

`]`

`xx = np.linspace(-5,5,100)`

`# HERE'S HOW YOU DO SUBPLOTS IN DATABRICKS`

`fig, axes = plt.subplots(1, 3, figsize=(20,5))`

`for func, ax in zip(functions, axes):`

` for point in points:`

` ax.plot(*point, 'o',c='red', markersize=5)`

` ax.plot(xx, func(xx))`

` ax.vlines(x,y,func(x))`

` ax.set_xlim(-5,5)`

` ax.set_ylim(-5,5)`

`display(fig)`

Minimally stated

`fig, axes = plt.subplots(1, n) # n = number of subplots`

`for ax in axes:`

` ax.plot(#something)`

`display(fig)`

**Answer** by ethancampbell
·
May 17, 2019 at 05:25 AM

Well yes, that is what my one is doing, but what I want to do is plot each set of data on a different graph if that makes sense I thought you meant overplayed! Call figure() between them. That'll create a completely https://www.prodissertationhelp.co.uk/ separate figure. Do I just straight up put figure() in a new line below the plot? Or is it something I have to import?

Databricks Inc.

160 Spear Street, 13th Floor

San Francisco, CA 94105

info@databricks.com

1-866-330-0121

- Anonymous
- Sign in
- Create
- Ask a question
- Create an article
- Explore
- Topics
- Questions
- Articles
- Badges