In this simple tutorial, we’ll see how to create a remarkable Line chart in Python using one of the best APIs for data science and visualization, of course, I’m talking of Plotly a fantastic API for charts.
Line chart
As you’re probably aware, line charts display a series of data points (markers) connected by line segments. It is similar to a scatter plot except that the measurement points are ordered (typically by their x-axis value) and joined with straight line segments.
Often, line charts are used to visualize a trend in data over intervals of time (a.k.a time series). No more words! Let’s jump to code!
Line chart in Plotly
In Plotly is easy to create wonderful customized charts, let’s see line charts in a few lines of codes
import plotly.offline as py import pandas as pd import numpy as np import plotly.graph_objects as go # Line Chart np.random.seed(52) # Create our x and y data x_values = np.linspace(0, 1, 100) y_values = np.random.randn(100) # In plotly the charts are named traces trace = go.Scatter(x = x_values, y = y_values + 5, mode = 'lines', name = 'markers') layout = go.Layout(title = 'My Simple Linechart', xaxis = {'title': 'Random x'}, yaxis = {'title': 'Random y'}) fig = go.Figure(data = trace, layout = layout) py.plot(fig, filename='simple-linechart')
The previous one is the simplest line chart that you can make with Plotly through the module graph_objects. In particular, it is used the class Scatter to create a line chart passing the following, basic, parameters:
- X: is a list of numeric values to show along the x-axis
- Y: is a list of numeric values to show along the y-axis
- Mode: define the modality of the draw. Differently from a scatter plot, now we pass the value “line”
You can refer to the documentation to discover the several parameters that the Scatter class has to customize your chart.
Here a brief tutorial on Scatter Plot
Sophisticated Line chart
Now, let’s try to customize our chart playing with parameters.
import pandas as pd import numpy as np import plotly.graph_objects as go import plotly.offline as py np.random.seed(42) # Create our x and y data x_values = np.linspace(0, 1, 100) y_values = np.random.randn(100) # Create a list of text labels to show on the chart # In particolar I want to show only the max and the min value text_values = ['' for x in x_values] text_values[np.argmax(y_values)] = round(y_values.max(), 2) text_values[np.argmin(y_values)] = round(y_values.min(), 2) trace = go.Scatter(x=x_values, y=y_values, mode='lines+markers+text', # Connect the points and add the text to line chart selectedpoints=[np.argmin(y_values), np.argmax(y_values)], # Select the min and max value selected=dict(marker=dict(color='tomato', size=15)), # Change the Style of the selected unselected=dict(marker=dict(size=1)), # Change the Style of the unselected text=text_values, # Set the text of the points textfont=dict(size=12), # Change the size of the text textposition='middle right',# Position of the text line={'shape': 'spline', 'smoothing': 1.3}) # Make the line chart more smooth layout = go.Layout(title='Sophisticated Line Chart', xaxis={'title': 'Random X'}, yaxis={'title': 'Random Y'}, height=800) fig = go.Figure(data=trace, layout=layout) #pyo.plot(fig) py.plot(fig, filename='linechart2')
Furthermore, you can notice that this chart is a little bit more sophisticated than the first one. In effect, we added more information to the chart playing with parameters of the Scatter class. Just a few comments on the code.
Lines Markers and Text
First, as in the previous chart, we passed our data and set lines+markers+text as the mode. In other words, this permits us to connect the markers with a line and add the text.
x =x_values, y = y_values, mode = 'lines+markers+text', # Connect the points and add the text to line chart
Selecting the Points
Second, we select the maximum and the minimum value of the distribution passing a list of points index to the parameter selected points, below the details:
- SelectedPoints: Array containing integer indexes of selected points. Has an effect only for traces that support selections
selectedpoints=[np.argmin(y_values), np.argmax(y_values)]
In particular, to get the index of the minimum and maximum values we use respectively the method argmin and argmax of the numpy
text_values[np.argmax(y_values)] = round(y_values.max(), 2) text_values[np.argmin(y_values)] = round(y_values.min(), 2)
Define markers’ style
Third, we define the style of the selected and unselected points changing the colour and the size. To clarify, here are the details of the parameters:
- Selected: Instance or dict with compatible properties. One of the properties is marker that define the style of the selected points
- Unselected: Instance or dict with compatible properties. One of the properties is the marker that define the style of the unselected points
selected=dict(marker=dict(color='tomato', size=15)) # Change the Style of the selected unselected=dict(marker=dict(size=1)), # Change the Style of the unselected
Note: We set the size of the unselected to one to highlight the maximum and minimum values only. All the possible configurations can be discovered in the documentation.
Conclusion
To conclude, Plotly is an easy-to-use library to create wonderful and interactive visualization and consequently beautiful dashboards.
See you in the next article!
You might also like
More from Data Visualisation
Plotly – Bar Chart Beautiful Ways Show Category data in Python
Quick tutorial starting from the most simple bar chart to create high-quality bar chart using Python and Plotly
Plotly – Make your scatterplot wonderful in Python
Easy way to create a wonderful scatter plot in Python with Plotly.