After our first steps with the interactive plots of Plotly, we will now explore the different diagrams we can use to visualize our data.
This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.
Line plot / Line chart
We can create a line plot with the line() method of Plotly Express:
1 2 3 4 5 |
fig = px.line(x=["a","b","c", "d"], y=[1,3,2,5], title="line plot", width=700) fig.show() |
This creates us a basic line plot:
Bar chart
For the categorical values, we can create a bar chart with the bar() method:
1 2 3 4 5 |
fig = px.bar(x=["a","b","c", "d"], y=[1,3,2,5], title="bar plot", width=700) fig.show() |
This gives us a vertical bar for each of the values:
Scatter plot
The scatter() method allows us to create a plot with dots for our values:
1 2 3 4 5 6 7 8 9 |
df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", width=700, height=500) fig.show() |
This turns the built-in data set iris into a scatter plot:
Histogram
We can create a histogram with the histogram() method:
1 2 3 4 |
tips = px.data.tips() fig = px.histogram(tips, x="total_bill") fig.show() |
This shows us the distribution of the values:
Box plot
With the box() method we can get our statistical box plots:
1 2 3 4 5 |
fig = px.box(tips, x="time", y="total_bill", color="day") fig.show() |
By specifying the value of day for the colour, we get the separation of time and day in one go for our box plot:
Pie chart
To create a pie chart, we can use the pie() method:
1 2 3 4 |
fig = px.pie(tips, values='tip', names='day') fig.show() |
This creates a pie chart, but the values on the pie are not always easy to read:
Next
These basic plots allow us to visualize our data in most cases. However, sometimes that is not enough. Plotly comes with its own specific diagrams that we explore next week.