Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Data Visualization Cookbook (Second Edition)

You're reading from   Python Data Visualization Cookbook (Second Edition) Visualize data using Python's most popular libraries

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781784396695
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Preparing Your Working Environment FREE CHAPTER 2. Knowing Your Data 3. Drawing Your First Plots and Customizing Them 4. More Plots and Customizations 5. Making 3D Visualizations 6. Plotting Charts with Images and Maps 7. Using the Right Plots to Understand Data 8. More on matplotlib Gems 9. Visualizations on the Clouds with Plot.ly Index

Plotting a 3D trefoil knot


In this recipe, we will see how to plot a 3D trefoil knot. A trefoil knot is a closed curve with three crossings. In this recipe, we will draw not just a curve, but a solid 3D curve. This is beyond the plotly capabilities, and we will implement this functionality using a trick.

How to do it...

In this recipe, we will:

  1. Generate all the points of the knot using parametric equations.

  2. Organize the data as required by plotly.

  3. Define a clean layout.

  4. Invoke plotly to draw the chart:

    import numpy as np
    import plotly.plotly as py
    from plotly.graph_objs import Scatter3d, Data, Layout
    from plotly.graph_objs import Figure, Line, Margin, Marker
    
    from numpy import linspace,pi,cos,sin
    phi = linspace(0,2*pi,250)
    x = sin(phi)+2*sin(2*phi)
    y = cos(phi)-2*cos(2*phi)
    z = -sin(3*phi)
    
    traces = list()
    colors = ['rgb(%d,50,210)' % c for c in np.abs(z / max(z)) * 255]
    for i in linspace(-np.pi,np.pi,50):
        trace = Scatter3d(x=x+np.cos(i)*.5, y=y+np.sin(i)*.5, z=z,
                           mode...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime