plotly

Plotly Set the Line Color

Line Plots are one of the most famous plots when working with sequential data. They allow us to visualize data as specific points above a cartesian plane. This can help you visualize how the data changes for a particular parameter. Plotly is a great data visualization that can help you create simple to complex plots. In this tutorial, we will learn how you can customize a line plot by setting the line color.”

Using Plotly Express

Before we learn how to customize a line plot, let us start by creating a simple plot. For this, we will use the natural_gas_prices.csv file.

You can download the required files from the data hub as shown in the link below:

https://datahub.io/core/natural-gas

Once downloaded, we can create a line plot with the monthly prices as shown in the code below:

import plotly.express as px
import pandas as pd
df = pd.read_csv("monthly.csv")
fig = px.line(df, x="Month", y="Price")
fig.show()

We start by importing the plotly express package as px in the code above. We then import pandas to read the CSV data and store it into a data frame.

Next, we use the px.line function to create a line plot. Again, we pass the target data, “Month” and “Price,” as x and y parameters, respectively.

The code above should return a plot as shown below:

Customizing Line Plot

If we use plotly express, we can customize the color of a line plot using the update_traces() function.

The update_traces() function allows us to add or remove features from an existing plot. This makes it easy to pass properties and values on customizing their plots.

Hence, we can call the update_traces() function and pass the line_color parameter as shown:

fig.update_traces(line_color="#32CD32")

Here, we pass the value of the target color as an RGB value. The resulting plot is as shown:

You can also pass Plotly defined color schemes as shown:

fig.update_traces(line_color="firebrick")

The code above should result in a figure as shown:

Using color_discrete_sequence

Another method you can use to customize a line plot’s color is the color_discrete_sequence parameter. This allows you to pass a list of color values that are assigned to each plot type.

For example, to customize the line color in our previous example, we can run the code:

fig = px.line(df, x="Month", y="Price",
    color_discrete_sequence=['red'])
fig.show()

This should set the color in the list as the default line color for your plot.

Using Plotly graph_objects

One of the best ways to dive into plot customization is using Plotly graph_objects. This class allows you to specify various properties to create custom plots.

To set the line color using graph_objects, we can pass the color parameter as a dictionary.

Let us take a simple example as shown:

import plotly.graph_objects as go
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
         'August', 'September', 'October', 'November', 'December']
prices= [2.33,4.22,3.86,3.22,2.34,4.88,4.45,3.12, 2.33,1.98,4.20,5.00,3.33]

fig = go.Figure(
    go.Scatter(x=months, y=prices)
)
fig.show()

In the example above, we use plotly graph_objects and the Scatter class to plot a simple line graph.

The resulting figure is as shown:

To update the line color, we can set the color as dictionary shown in the example below:

fig = go.Figure()
fig.add_trace(go.Scatter(x=months, y=prices, line=dict(color='#32CD32')))
fig.show()

In the example above, we start by creating an empty Figure using the go.Figure() class. We then use the add_trace function to create a scatter plot and pass the target values we wish to include.

In our case, we specify the color as lime green, as shown in the plot.

And with that, you have successfully learned how to set the plot line color in Plotly.

Thanks for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list