Data Visualization With Python
Data Visualization With Python
Python provides various libraries that come with different features for visualizing data.
All these libraries come with different features and can support various types of graphs.
In this tutorial, we will be discussing four such libraries.
• Matplotlib
• Seaborn
• Bokeh
• Plotly
• Database Used
• Tips Database
• Tips database is the record of the tip given by the customers in a restaurant for
two and a half months in the early 1990s. It contains 6 columns such as
total_bill, tip, sex, smoker, day, time, size.
• You can download the tips database from here.
import pandas as pd
plt.show()
This graph can be more meaningful if we can add colors and also change the size of
the points. We can do this by using the c and s parameter respectively of the scatter
function. We can also show the color bar using the colorbar() method.
Example:
import pandas as pd
import matplotlib.pyplot as plt
plt.colorbar()
plt.show()
Line Chart
Line Chart is used to represent a relationship between two data X and Y on a different
axis. It is plotted using the plot() function. Let’s see the below example.
Example:
import pandas as pd
import matplotlib.pyplot as plt
# reading the database
data = pd.read_csv("tips.csv")
plt.show()
Bar Chart
A bar plot or bar chart is a graph that represents the category of data with rectangular
bars with lengths and heights that is proportional to the values which they represent. It
can be created using the bar() method.
Example:
import pandas as pd
import matplotlib.pyplot as plt
plt.title("Bar Chart")
# Setting the X and Y labels
plt.xlabel('Day')
plt.ylabel('Tip')
Histogram
A histogram is basically used to represent data in the form of some groups. It is a type
of bar plot where the X-axis represents the bin ranges while the Y-axis gives
information about frequency. The hist() function is used to compute and create a
histogram. In histogram, if we pass categorical data then it will automatically compute
the frequency of that data i.e. how often each value occurred.
import pandas as pd
import matplotlib.pyplot as plt
# reading the database
data = pd.read_csv("tips.csv")
# histogram of total_bills
plt.hist(data['total_bill'])
plt.title("Histogram")
Seaborn
Seaborn is a high-level interface built on top of the Matplotlib. It provides beautiful
design styles and color palettes to make more attractive graphs.
To install seaborn type the below command in the terminal.
pip install seaborn
Seaborn is built on the top of Matplotlib, therefore it can be used with the Matplotlib as
well. Using both Matplotlib and Seaborn together is a very simple process. We just
have to invoke the Seaborn Plotting function as normal, and then we can use
Matplotlib’s customization function.
Note: Seaborn comes loaded with dataset such as tips, iris, etc. but for the sake of
this tutorial we will use Pandas for loading these datasets.
Example:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# draw lineplot
sns.lineplot(x="sex", y="total_bill", data=data)
plt.show()
Scatter Plot
Scatter plot is plotted using the scatterplot() method. This is similar to Matplotlib, but
additional argument data is required.
Example:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
You will find that while using Matplotlib it will a lot difficult if you want to color each
point of this plot according to the sex. But in scatter plot it can be done with the help of
hue argument.
Example:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
Line Plot in Seaborn plotted using the lineplot() method. In this, we can pass only the
data argument also.
Example:
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
sns.barplot(x='day',y='tip', data=data,
hue='sex')
plt.show()
Histogram
# importing packages
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
After going through all these plots you must have noticed that customizing plots using
Seaborn is a lot more easier than using Matplotlib. And it is also built over matplotlib
then we can also use matplotlib functions while using Seaborn.
Bokeh
Let’s move on to the third library of our list. Bokeh is mainly famous for its interactive
charts visualization. Bokeh renders its plots using HTML and JavaScript that uses
modern web browsers for presenting elegant, concise construction of novel graphics
with high-level interactivity.
To install this type the below command in the terminal.
pip install bokeh
Scatter Plot
Scatter Plot in Bokeh can be plotted using the scatter() method of the plotting module.
Here pass the x and y coordinates respectively.
Example:
A line plot can be created using the line() method of the plotting module.
Example:
Bar Chart can be of two types horizontal bars and vertical bars. Each can be created
using the hbar() and vbar() functions of the plotting interface respectively.
Example:
# importing the modules
from bokeh.plotting import figure, output_file, show
import pandas as pd
# instantiating the figure object
graph = figure(title = "Bokeh Bar Chart")
One of the key features of Bokeh is to add interaction to the plots. Let’s see various
interactions that can be added.
Interactive Legends
click_policy property makes the legend interactive. There are two types of interactivity
–
• Hiding: Hides the Glyphs.
• Muting: Hiding the glyph makes it vanish completely, on the other hand,
muting the glyph just de-emphasizes the glyph based on the parameters.
Example:
graph.vbar(data['tip'], top=data['size'],
legend_label = "Tips VS Size", color='red')
graph.legend.click_policy = "hide"
button = Button(label="GFG")
button.js_on_click(CustomJS(
code="console.log('button: click!', this.toString())"))
checkbox_group.js_on_click(CustomJS(code="""
console.log('checkbox_group: active=' + this.active, this.toString())
"""))
radio_group.js_on_click(CustomJS(code="""
console.log('radio_group: active=' + this.active, this.toString())
"""))
show(button)
show(checkbox_group)
show(radio_group)
slider.js_on_change("value", CustomJS(code="""
console.log('slider: value=' + this.value, this.toString())
"""))
show(slider)
Similarly, much more widgets are available like a dropdown menu or tabs widgets can
be added.
Plotly
This is the last library of our list and you might be wondering why plotly. Here’s why –
• Plotly has hover tool capabilities that allow us to detect any outliers or
anomalies in numerous data points.
• It allows more customization.
• It makes the graph visually more attractive.
To install it type the below command in the terminal.
pip install plotly
Scatter Plot
Scatter plot in Plotly can be created using the scatter() method of plotly.express. Like
Seaborn, an extra data argument is also required here.
Example:
import plotly.express as px
import pandas as pd
Line plot in Plotly is much accessible and illustrious annexation to plotly which manage
a variety of types of data and assemble easy-to-style statistic. With px.line each data
position is represented as a vertex
Example:
import plotly.express as px
import pandas as pd
Bar Chart in Plotly can be created using the bar() method of plotly.express class.
Example:
import plotly.express as px
import pandas as pd
import plotly.express as px
import pandas as pd
Just like Bokeh, plotly also provides various interactions. Let’s discuss a few of them.
Creating Dropdown Menu: A drop-down menu is a part of the menu-button which is
displayed on a screen all the time. Every menu button is associated with a Menu
widget that can display the choices for that menu button when clicked on it. In plotly,
there are 4 possible methods to modify the charts by using updatemenu method.
• restyle: modify data or data attributes
• relayout: modify layout attributes
• update: modify data and layout attributes
• animate: start or pause an animation
import plotly.graph_objects as px
import pandas as pd
plot = px.Figure(data=[px.Scatter(
x=data['day'],
y=data['tip'],
mode='markers',)
])
# Add dropdown
plot.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=["type", "scatter"],
label="Scatter Plot",
method="restyle"
),
dict(
args=["type", "bar"],
label="Bar Chart",
method="restyle"
)
]),
direction="down",
),
]
)
plot.show()
Adding Buttons: In plotly, actions custom Buttons are used to quickly make actions
directly from a record. Custom Buttons can be added to page layouts in CRM,
Marketing, and Custom Apps. There are also 4 possible methods that can be applied
in custom buttons:
• restyle: modify data or data attributes
• relayout: modify layout attributes
• update: modify data and layout attributes
• animate: start or pause an animation
Example:
import plotly.graph_objects as px
import pandas as pd
plot = px.Figure(data=[px.Scatter(
x=data['day'],
y=data['tip'],
mode='markers',)
])
# Add dropdown
plot.update_layout(
updatemenus=[
dict(
type="buttons",
direction="left",
buttons=list([
dict(
args=["type", "scatter"],
label="Scatter Plot",
method="restyle"
),
dict(
args=["type", "bar"],
label="Bar Chart",
method="restyle"
)
]),
),
]
)
plot.show()
Creating Sliders and Selectors:
In plotly, the range slider is a custom range-type input control. It allows selecting a
value or a range of values between a specified minimum and maximum range. And
the range selector is a tool for selecting ranges to display within the chart. It provides
buttons to select pre-configured ranges in the chart. It also provides input boxes where
the minimum and maximum dates can be manually input
Example:
import plotly.graph_objects as px
import pandas as pd
plot = px.Figure(data=[px.Scatter(
y=data['tip'],
mode='lines',)
])
plot.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
step="day",
stepmode="backward"),
])
),
rangeslider=dict(
visible=True
),
)
)
plot.show()
https://www.geeksforgeeks.org/matplotlib-tutorial/
https://www.geeksforgeeks.org/python-seaborn-tutorial/
https://www.geeksforgeeks.org/python-bokeh-tutorial-interactive-data-visualization-
with-bokeh/
https://www.geeksforgeeks.org/python-plotly-tutorial/