Plot graphs and data in a notebook
Use plot cells to draw functions, 3D surfaces, scatter plots with a fitted line, and histograms.
A plot cell holds one drawing command. Run it and the picture appears under the cell. Plots can be dragged to move around and zoomed.
- Add a Plot cell. To graph functions, type plot(sin(x), x, -2π, 2π). You can list several functions, and the numbers after x set the window. Add two more numbers for the y range.
- For a 3D surface type plot3d(x^2-y^2). Add ranges if you like: plot3d(f(x,y), x, a, b, y, c, d).
- To plot data first add a Data cell and paste CSV or TSV text (the first row holds column names). Give the table a name, such as s. You can sort it and filter it, for example height > 150.
- Then use a plot cell with the columns: scatter(s.hours, s.score, lin) draws points with a straight-line fit. Replace lin with quad or exp for other fits, or resid for a residual plot. hist(s.score, 4) draws a histogram with 4 bars.
- Run the cell. Drag the plot to pan, use the zoom in and zoom out buttons or pinch to zoom, and use Reset view to come back.
- If a cell shows a red message, read it, fix the command and run again.
Tips
- Functions you defined in math cells (Define f(x)=x^2) can be used in plot cells, for example plot(f(x), x, -3, 3).
- In a math cell, linreg(s.hours, s.score) gives the fitted line as numbers.
- The Statistics example notebook shows data, a fitted line and a residual plot working together.
Related guides
- Start a notebookThe Notebook is a page of text, math, plot and data cells that you can run, edit, save and share.
- Save, export and print a notebookSave a notebook file, export it as Markdown or a web page, or print it to paper or PDF.
- Draw a scatter plotPlot paired data as dots so you can see whether a pattern is there.