Programmingempire
In this post on Creating Basic Charts using Plotly, you will learn to create a basic chart using a python package called Plotly.
About Plotly
Basically, it is a graphing library in python language that allows people to create interactive charts. The library has provision for creating many types of charts ranging from basic charts to scientific ones. For example, we can create basic charts like scatter plots and line plots as well as we can also draw statistical, scientific, financial, and Machine Learning charts. Moreover, there is also a provision for creating Maps, 3D Charts, and Subplots.
Creating Basic Charts using Plotly
In particular, we can draw the following types of basic charts using Plotly.
- Scatter Plot
- Line Charts
- Bar Charts
- Bubble Charts
- Gantt Charts
- Filled Area Charts
- Sunburst Charts
- Treemap Charts
- Dot Plots
Benefits of Creating Charts using Plotly
- We can share the charts online
- These charts are compatible with a number of languages
- Interactive visualization is possible
Examples of Creating Basic Charts using Plotly
The following examples show how to create the above-mentioned basic charts. Before starting with plots you need to install the Plotly library by running the following command.
pip install plotly
To illustrate Plotly charts we use a Novel Corona Virus 2019 Dataset which is available here. As can be seen by running following code, our dataset has following fields.
[‘SNo’, ‘ObservationDate’, ‘Province/State’, ‘Country/Region’, ‘Last Update’, ‘Confirmed’, ‘Deaths’, ‘Recovered’]
Provided that you have already installed plotly package, you can now read the dataset from the CSV file and fetch it in a data frame. Also, display the attributes of the dataset using the list() function.
import pandas as pd
df=pd.read_csv("covid_19_data.csv")
print(list(df))
After that, we determine the correlation between different fields of the dataset using following code. Accordingly, we create the following four charts using our dataset.
Scatter Plot
Basically, a scatter plot shows the correlation between two variables. In other words, the scatter plot displays the data points for two variables in x and y-axis. The following example shows the correlation between the confirmed and recovered cases. Moreover, each country is represented by the color attribute.
fig = px.scatter(df, x="Confirmed", y="Recovered", color="Country/Region")
Line Plot
In similar way, the line plot shows the relationship between two variables using a line. Also, the color attributes represents the different countries.
fig = px.line(df, x="Confirmed", y="Recovered", color="Country/Region")
Bar Chart
As can be seen, a bar chart compares the coronavirus cases for each country. However, we need to group the data before plotting a chart. Therefore, first, we apply() the groupby() function on the Country/Region field. After that, we plot the bar chart. Additionally, we can change the colors of the chart using the update_traces() function.
import plotly.express as px
import pandas as pd
df=pd.read_csv("covid_19_data.csv")
print(list(df))
df=df.dropna()
df1=df.groupby("Country/Region")['Confirmed'].sum().to_frame()
print(df1)
fig = px.bar(df, x="Country/Region", y="Confirmed", title="Country-wise Confirmed Covid-19 Cases")
fig.update_traces(marker_color='rgb(19,10,12)', marker_line_color='rgb(248,2,12)',
marker_line_width=1.5, opacity=0.8)
fig.show()
Pie Chart
Similarly, we can draw a circular statistical chart called as the pie chart. In essence, the slices of the pie chart represent the countries and the vales denote the confirmed coronavirus cases.
import plotly.express as px
import pandas as pd
df=pd.read_csv("covid_19_data.csv")
print(list(df))
fig = px.pie(df, values='Confirmed', names="Country/Region", title='Country-wise Coronavirus Confirmed Cases in the World')
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()
Summary
In this article on Creating Basic Charts using Plotly, I have demonstrated how to use the Plotly library for plotting the basic charts. Certainly, this library has advantages over other such libraries since we can use these charts easily in our application. Moreover, the charts are interactive and can be used to create attractive and insightful data analytics interfaces.
Further Reading
How to Implement Inheritance in Python
Find Prime Numbers in Given Range in Python
Running Instructions in an Interactive Interpreter in Python
Deep Learning Practice Exercise
Deep Learning Methods for Object Detection
Image Contrast Enhancement using Histogram Equalization
Transfer Learning and its Applications
Examples of OpenCV Library in Python
Understanding Blockchain Concepts
Example of Multi-layer Perceptron Classifier in Python
Measuring Performance of Classification using Confusion Matrix
Artificial Neural Network (ANN) Model using Scikit-Learn
Popular Machine Learning Algorithms for Prediction
Long Short Term Memory – An Artificial Recurrent Neural Network Architecture
Python Project Ideas for Undergraduate Students
Creating Basic Charts using Plotly
Visualizing Regression Models with lmplot() and residplot() in Seaborn
Data Visualization with Pandas