We have previously covered how to create data visualizations using the plotly package.
In that introductory chapter, we passed simple lists to the chart-making functions, however the plotly package provides an easy-to-use, intuitive interface when working with tabular data.
Now that we know how to work with DataFrame objects, let’s revisit each of the previous examples, but this time using tabular data.
7.0.1 Line Charts, Revisited
Starting with some example data, like before, this time we construct a DataFrame object from the data (because the data is in an eligible format, in this case a list of dictionaries):
If we construct a DataFrame from this data, we get to skip the mapping step, and move directly to the chart-making step.
Now we have a few options about how to pass this data to the chart-making function. We can use a Series oriented approach, or a DataFrame oriented approach.
7.0.1.1Series Oriented Approach
In the Series oriented approach, we pass the columns to the chart-making function, because when representing a column, the series is list-like:
from plotly.express import linefig = line(x=df["date"], y=df["stock_price_usd"], height=350, title="Stock Prices over Time", labels={"x": "Date", "y": "Stock Price ($)"})fig.show()
7.0.1.2DataFrame Oriented Approach
Alternatively, we can use a DataFrame oriented approach where we pass the DataFrame as the first parameter to the chart-maker function.
from plotly.express import linefig = line(df, x="date", y="stock_price_usd", height=350, title="Stock Prices over Time", labels={"date": "Date", "stock_price_usd": "Stock Price ($)"})fig.show()
Notice, when we pass the DataFrame as the first parameter, now the x and y parameters refer to string column names in that DataFrame to be plotted on the x and y axis, respectively. The labels parameter keys now reference the column names as well.
For the remaining examples, we will use this DataFrame oriented approach.
from plotly.express import barfig = bar(df, x="genre", y="viewers", height=350, title="Viewership by Genre", labels={"genre": "Genre", "viewers": "Viewers"})fig.show()
7.0.2.1 Horizontal Bar Chart, Revisited
With categorical data, a horizontal bar chart can be a better choice than a vertical bar chart. Ideally, the bars are sorted so the largest are on top. This helps tell the story of which are the “top genres”.
Before charting, we use a pandas sorting operation to get the bars in the right order: