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(-2*np.pi, 2*np.pi, 50)
    
    trace0 = Scatter(x=x.tolist(),
        y=np.sin(x).tolist(),
        name='sin(x)'
    )
    trace1 = Scatter(
        x=x.tolist(),
        y=np.cos(x).tolist(),
        name='cos(x)'
    )
    
    data = [trace0, trace1]
    
    unique_url = py.plot(data, filename = 'sin-cos')

How it works...

Here, we used the Scatter object twice. The first instance of Scatter (trace0) represents the points of the sine curve, while the second instance (trace1) represents the cosine curve. The parameters x and y of the constructor of these objects are used to specify the data points to plot, while the parameter name is used for the legend of the chart.

After building the objects that represent the curves, we wrapped all the data in a list that was then passed to the py.plot method. This method invokes Plot.ly and creates the chart for us. Right after invoking this method, your default browser opens automatically on a page that shows the chart. Here's what I got:

How it works...

Now, the chart (and also the data) is stored on the Plot.ly servers, and we can access it through the URL returned by the method plot. Every chart has a unique name specified through the parameter filename of the method plot (in this case, the name of the chart is sin–cos).

The peculiarity of the Plot.ly interface is that the charts are interactive. If we hover the mouse pointer on the curves, we see a tooltip box that shows the information of the point we're on. We can also zoom out and zoom in using the scrolling wheel of the mouse.

There's more...

Following the link DATA, we also have the opportunity to inspect the data:

There's more...

This feature enables us to share the chart and the data at the same time. If we go into the CODE section, we can also view the data in the JSON format:

There's more...

We even get a script (in Python, MATLAB, Javascript, R, or Julia) that recreates the chart. Here's the Python version:

There's more...

Also, going into the Edit graph section, we are able to modify many aspects of the chart, such as the theme and layout, and to add notes and so on without going back to the code:

There's more...

This very handy because it allows us to sketch a chart with a very simple code snippet and then improve its appearance with a point-and-click interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.108.112