Pandas + Plotly (ft. TensorFlow)

Cufflinks ( is a simple library that connects Plotly with Pandas to produce beautiful and interactive visualizations 🌈

Why Cufflinks instead of Plotly?

  • Cufflinks is used to link plotly to pandas, and...
  • The difference between activating this and "normal" plots (e.g. df.plot(...)) is minimal ⁠— the code is virtually the same.
  • Hence, you get the power of plotly, but with less code.

Here's a short demo, featuring a linear regression model using TensorFlow.

1. Import dependencies

import tensorflow as tf
import cufflinks as cf
import pandas as pd
import numpy as np

# For cufflinks: switch to offline mode
# (that way we don't need to sign in to Plotly)

# Check if there's an external NVIDIA GPU in the system 
if tf.test.is_gpu_available():
    print('Using GPU:', tf.test.gpu_device_name())

Because I have a GTX 1060 in my system, I see the following line:

Using GPU: /device:GPU:0

2. Create random data points and plot them

x_train = np.linspace(-1, 1, 101)                                  
y_train = (2 * x_train) + (np.random.randn(*x_train.shape) * 0.33)

df = pd.DataFrame(dict(x_train=x_train, y_train=y_train))

fig = df.iplot(kind     = 'scatter', 
               x        = 'x_train', 
               y        = 'y_train', 
               mode     = 'markers', 
               xTitle   = 'x_train_title', 
               yTitle   = 'y_train_title', 
               name     = 'name',
               asFigure = True)

Here's the interactive(!!!) output:


3. Create the model (a.k.a. y_model)

X = tf.placeholder(tf.float32)                                             
Y = tf.placeholder(tf.float32)                                             

def model(X, w):                                                           
    return tf.multiply(X, w)

w = tf.Variable(0.0, name="weights")                                       

y_model = model(X, w)

4. Define the cost function

# Here, we use the squared error, 
# where error = actual_values - predicted_values
cost = tf.square(Y - y_model)                                                

5. Define the optimizer train_op

# Here, we'll use Gradient Descent
train_op = tf.train.GradientDescentOptimizer(0.001).minimize(cost) 

6. Start the training

# Initialize the session `sess`
sess = tf.Session()                                                        
init = tf.global_variables_initializer()                                                                                       

for epoch in range(10):                                       
    for (x, y) in zip(x_train, y_train):                                    , feed_dict={X: x, Y: y})                             

w_val =                                                        

# ...and remember to close the session with `sess.close()`

7. And finally, PLOT THE D@MN THING!

from plotly.graph_objs import Scatter

y_learned = x_train * w_val  

new_trace = Scatter(x    = x_train, 
                    y    = y_learned, 
                    name = 'What I\'ve Learnt')



If you enjoyed this post and want to buy me a cup of coffee...

The thing is, I'll always accept a cup of coffee. So feel free to buy me one.

Cheers! ☕️