Book Image

Python Data Visualization Cookbook (Second Edition)

Book Image

Python Data Visualization Cookbook (Second Edition)

Overview of this book

Python Data Visualization Cookbook will progress the reader from the point of installing and setting up a Python environment for data manipulation and visualization all the way to 3D animations using Python libraries. Readers will benefit from over 60 precise and reproducible recipes that will guide the reader towards a better understanding of data concepts and the building blocks for subsequent and sometimes more advanced concepts. Python Data Visualization Cookbook starts by showing how to set up matplotlib and the related libraries that are required for most parts of the book, before moving on to discuss some of the lesser-used diagrams and charts such as Gantt Charts or Sankey diagrams. Initially it uses simple plots and charts to more advanced ones, to make it easy to understand for readers. As the readers will go through the book, they will get to know about the 3D diagrams and animations. Maps are irreplaceable for displaying geo-spatial data, so this book will also show how to build them. In the last chapter, it includes explanation on how to incorporate matplotlib into different environments, such as a writing system, LaTeX, or how to create Gantt charts using Python.
Table of Contents (16 chapters)
Python Data Visualization Cookbook Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Creating line charts


In this recipe, we will see how to create a line chart. We have already introduced this kind of chart in Chapter 3, Drawing Your First Plots and Customizing Them, and we have seen how to make the plots with matplotlib. This time we'll focus on how to create and share them with Plot.ly.

Getting ready

Before starting, you need to set up your credentials for the Plot.ly platform in the programming environment:

$ python -c "import plotly; plotly.tools.set_credentials_file(username='DemoAccount', api_key='mykey')"

Replace Demo Account and mykey with your Plotly username and API key.

How to do it...

The following code example demonstrates how to plot two curves. In particular, we will:

  1. Generate the data to plot (a sine and a cosine wave).

  2. Organize the data in the format required by Plot.ly.

  3. Send a request to the server.

  4. Receive a URL that points to our chart.

  5. Run the following code:

    import plotly.plotly as py
    from plotly.graph_objs import Scatter
    import numpy as np
    
    x = np.linspace...