# Conversion of matplotlib contour plots to plotly contour plots

**URL:** <https://community.plotly.com/t/conversion-of-matplotlib-contour-plots-to-plotly-contour-plots/18645>\
**Category:** 📊 Plotly Python\
**Created:** [January 27, 2019, 3:54pm UTC](https://community.plotly.com/t/conversion-of-matplotlib-contour-plots-to-plotly-contour-plots/18645 "2019-01-27T15:54:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![acesaif](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/acesaif/32/5496_2.png) [@acesaif](https://community.plotly.com/u/acesaif)\
**Post date:** [January 27, 2019, 3:54pm UTC](https://community.plotly.com/t/conversion-of-matplotlib-contour-plots-to-plotly-contour-plots/18645/1 "2019-01-27T15:54:36Z")

</div>

Actually I am trying to plot Hyperbola in `plotly`. For that I first converted `matplotlib` hyperbola plot to `plotly` plot. And I faced a `UserWarning` from `plotly`.

This is my `matplotlib` code,

```auto
x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
x, y = np.meshgrid(x, y)

range_ = [-6.0, -5.0]
a = range_[0]
b = range_[1]

plt.figure(figsize=(10, 5))
plt.contour((x **2/a** 2 - y **2/b** 2), [1], colors='g')
plt.show()

```

This gave me nice Hyperbola plot. Hence I wanted to make the same plot in `plotly`.

Here is my `plotly` version of the same code.

```auto
hyper = plt.gcf() # this is an object of matplotlib plot.

plot_hyper = tls.mpl_to_plotly(hyper)

layout = go.Layout(
    title='Hyperbola',
    font=dict(family='IBM Plex Mono, monospace', color='black'),
    plot_bgcolor='#fafafa',
    paper_bgcolor='#fafafa',
    width=800,
    xaxis=dict(
        zeroline=False,
        tickfont=dict(
            family='Crimson Text, serif',
            size=15,
            color='black'
        ),
    ),
    yaxis=dict(
        zeroline=False,
        tickfont=dict(
            family='Crimson Text, serif',
            size=15,
            color='black'
        ),
    )
)

plot_hyper['layout'] = layout
py.iplot(plot_hyper)

```

This gives me an empty plot. Please guide me to attain this. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [January 28, 2019, 10:51am UTC](https://community.plotly.com/t/conversion-of-matplotlib-contour-plots-to-plotly-contour-plots/18645/2 "2019-01-28T10:51:13Z")

</div>

Hi @acesaif,

I’d recommend building the figure with the plotly `contour` trace type directly ([https://plot.ly/python/contour-plots/](https://plot.ly/python/contour-plots/)). Here’s an example that i think it pretty close to the matplotlib version

```python
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

x = np.linspace(-10, 10, 400)
y = np.linspace(-5, 5, 400)
x, y = np.meshgrid(x, y)

a = 1
b = 2
z = x **2/a** 2 - y **2/b** 2

contour = go.Contour(
    z=z
)

contour.contours.type = 'constraint'
contour.contours.value = 1
data = [contour]

fig = go.Figure(data=data)
iplot(fig)

```

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/8/8070e9b9b8c1f1b0bd0e9dc2b9d937284f28af5b.png)

Hope that helps!  
-Jon

---

<div class="post-metadata">

**Author:** ![acesaif](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/acesaif/32/5496_2.png) [@acesaif](https://community.plotly.com/u/acesaif)\
**Post date:** [January 28, 2019, 12:45pm UTC](https://community.plotly.com/t/conversion-of-matplotlib-contour-plots-to-plotly-contour-plots/18645/3 "2019-01-28T12:45:18Z")

</div>

Thank you so much @jmmease. That is very helpful.
