> For the complete documentation index, see [llms.txt](https://jona-projects.gitbook.io/kiwicalc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jona-projects.gitbook.io/kiwicalc/analytic-geometry/graph2d.md).

# Graph2D

The `Graph2D` class enables you to plot many functions, expressions and geometric shapes extremely easily in matplotlib. You just create a `Graph2D` object, add the items you wish to plot, and plot them together. You can also scatter these items as well.

#### Properties

* `items` - a list of all of the Plottable items that were added to the `Graph2D` object

#### Create a new `Graph2D` object

The Graph2D constructor accepts a collection of objects to be plotted in 2D axis system. Here is the signature of the `__init__()` method:

```python
def __init__(self, objs: Iterable[IPlottable] = ()):                    
```

As you can see, `Graph2D` can only accept objects that inherit from `IPlottable`, namely, they have a matching `plot()` method.

Here is a list of the objects currently implementing this interface:

* `FastPoly`
* `Poly`
* `Log`
* `Ln`
* `Function`
* `FunctionCollection`
* `FunctionChain`
* `Point2D`
* `Circle`
* `Vector2D`

For example:

```python
my_graph = Graph2D((Function("f(x) = 2x"),Poly("x^3 - 3")))                   
```

In addition, you can also initialize the `Graph2D` object without anything:

```python
my_graph = Graph2D()          
```

Until plottable items are added, this `Graph2D` object will be considered empty and can't be plotted.

#### `add()`

You can add plottable objects to the graph via the `add()` method. For instance:

```python
my_graph = Graph2D()
my_graph.add(Function("f(x) = x^3-3"))
my_graph.add(Poly("2x^4 - 3x + 5"))
my_graph.add(Circle(5, (0, 0)))                   
```

#### `plot()`

You can plot all of the items via the `plot()` method. The method takes many optional parameters so you can customize your graph conveniently, however, you can also call the method without any parameters(the default values will be used). Here is the signature of the method:

```python
def plot(self, start: float = -10, stop: float = 10,
    step: float = 0.01, ymin: float = -10, ymax: float = 10, text=None, show_axis=True, show=True,
    formatText=False, values=None):               
```

Here are the parameters:

* `start` - initial x to plot from. Default: `-10`
* `stop` - last x value in the plot. Default: `10`
* `step` - the interval between each x. Default: `0.01`
* `ymin` - lowest y value in the initial scope Default: `-10`
* `ymax` - highest y value in the initial scope. Default: `10`
* `text` - customize the graph's title
* `formatText` - try to format the text to latex (currently experimental). Default: `False`
* `values` - instead of `start`, `stop` and `step` - you can just insert a list of x values to plot in. Default: `None`

For instance:

```python
my_graph = Graph2D()

my_graph.add(Function("f(x) = x^3-3"))
my_graph.add(Poly("2x^4 - 3x + 5"))
my_graph.add(Circle(5, (0, 0)))

my_graph.plot()
```

<figure><img src="/files/zZCyxLGCiC0zBejbalPs" alt=""><figcaption><p>Plotting several functions on the same plot, via the Graph2D class</p></figcaption></figure>
