As indicated earlier, numerous plotting formats can be used, even offline or online ones. The following are examples of direct plotting.
Listing 2-7 implements a basic plotting plot. Figure 2-13 shows the graph.
Listing 2-7. Running Basic Plotting
In [116]: import pandas as pd import numpy as np
df = pd.DataFrame(np.random.randn(200,6),index= pd.date_
range('1/9/2009', periods=200), columns= list('ABCDEF')) df.plot(figsize=(20, 10)).legend(bbox_to_anchor=(1, 1))
Listing 2-8 creates a bar plot graph (see Figure 2-14).
Listing 2-8. Direct Plotting In [123]: import pandas as pd import numpy as np Figure 2-13. Direct plot graph
df = pd.DataFrame(np.random.rand(20,5), columns=['Jan','Feb', 'March','April', 'May'])
df.plot.bar(figsize=(20, 10)).legend(bbox_to_anchor=(1.1, 1))
Listing 2-9 sets stacked=True to produce a stacked bar plot (see Figure 2-15).
Listing 2-9. Create a stacked bar plot In [124]: import pandas as pd
df = pd.DataFrame(np.random.rand(20,5), columns=['Jan','Feb', 'March','April', 'May']) df.plot.bar(stacked=True,
figsize=(20, 10)).legend(bbox_to_anchor=(1.1, 1)) Figure 2-14. Direct bar plot graph
To get horizontal bar plots, use the barh method, as shown in Listing 2-10.
Figure 2-16 shows the resulting graph.
Listing 2-10. Bar Plots
In [126]: import pandas as pd
df = pd.DataFrame(np.random.rand(20,5), columns=['Jan','Feb', 'March','April', 'May']) df.plot.barh(stacked=True,
figsize=(20, 10)).legend(bbox_to_anchor=(1.1, 1)) Figure 2-15. Stacked bar plot graph
Figure 2-16. Horizontal bar plot graph
Histograms can be plotted using the plot.hist() method; you can also specify the number of bins, as shown in Listing 2-11. Figure 2-17 shows the graph.
Listing 2-11. Using the Bar’s bins Attribute In [131]: import pandas as pd
df = pd.DataFrame(np.random.rand(20,5), columns=['Jan','Feb', 'March','April', 'May'])
df.plot.hist(bins= 20, figsize=(10,8)).legend bbox_to_anchor=(1.2, 1))
Listing 2-12 plots multiple histograms per column in the data set (see Figure 2-18).
Figure 2-17. Histogram plot graph
Listing 2-12. Multiple Histograms per Column In [139]: import pandas as pd
import numpy as np
df=pd.DataFrame({'April':np.random.randn(1000)+1,'May':np.random.
randn(1000),'June': np.random.randn(1000) - 1}, columns=['April', 'May', 'June'])
df.hist(bins=20)
Listing 2-13 implements a box plot (see Figure 2-19).
Listing 2-13. Creating a Box Plot In [140]:import pandas as pd import numpy as np
Figure 2-18. Column base histograms plot graph
df = pd.DataFrame(np.random.rand(20,5),
columns=['Jan','Feb','March','April', 'May']) df.plot.box()
Listing 2-14 implements an area plot (see Figure 2-20).
Listing 2-14. Creating an Area Plot In [145]: import pandas as pd import numpy as np
df = pd.DataFrame(np.random.rand(20,5),
columns= ['Jan','Feb','March','April', 'May']) df.plot.area(figsize=(6, 4)).legend
(bbox_to_anchor=(1.3, 1)) Figure 2-19. Box plot graph
Listing 2-15 creates a scatter plot (see Figure 2-21).
Listing 2-15. Creating a Scatter Plot In [150]: import pandas as pd import numpy as np
df = pd.DataFrame(np.random.rand(20,5),columns= ['Jan','Feb', 'March','April', 'May'])
df.plot.scatter(x='Feb', y='Jan', title='Temperature over two months ')
Figure 2-20. Area plot graph
See Chapter 7 for more graphing formats.
Summary
This chapter demonstrated how to implement data visualization in modern business. Let’s recap what you studied in this chapter.
– Understand the importance of data visualization.
– Acknowledge the usage of data visualization in modern business and its future implementations.
– Recognize the role of data visualization in decision-making.
Figure 2-21. Scatter plot graph
– Load and use important Python data visualization libraries.
– Revise exercises with model answers for practicing and simulating real-life scenarios.
The next chapter will cover data collection structure and much more.
Exercises and Answers
1. What is meant by data visualization?
Answer:
Data visualization is the process of interpreting the data in the form of pictorial or graphical format.
2. Why is data visualization important?
Answer:
Data Visualization helps business to achieve numerous goals through the following.
– Convert the business data into interactive graphs for dynamic interpretation to serve the business goals.
– Transforming data into visually appealing, interactive dashboards of various data sources to serve the business with the insights.
– Create more attractive and informative dashboard of various graphical data representation.
– Make appropriate decisions by drilling into the data and finding the insights.
– Figure out the patterns, trends and correlations in the data being analyzed to determine where they must improve their operational processes and thereby grow their business.
– Give full picture of the data under analysis.
– Enable to organize and present massive data intuitively to present important findings from the data.
– Make better, quick and informed decisions.
3. Why do modern businesses need data visualization?
Answer:
Data visualization is needed by the modern business to support the following areas.
– Analyze the business different processes where the management can focus on the areas of improvement to generate more revenue and improve productivity.
– Bring business intelligences to life.
– Apply creative approach to improve the abilities to understand the hidden information within the business data.
– Provide better and faster way to identify patterns,
trends, and correlation in the data sets that would remain undetected with a text.
– Identify new business opportunities by predicting upcoming trends or sales volumes and the revenue they would generate.
– Helps to spot trends in data that may not have been noticeable from the text alone.
– Supply managers with information they need to make more effective comparisons between data sets by plotting them on the same visualization.
– Enable managers to understand the correlations between the operating conditions and business performance.
– Help to discover the gray areas of the business and hence take right decisions for improvement.
– Helps to understand customers’ behaviors and interests, and hence retains customers and market.
4. How is data visualization used for business decision-making?
Answer:
There are many ways in which visualization help the business to improve decision making.
Faster Times Response: It becomes incredibly useful to put useful interpretation of the collected data into the hands of managers and decision makers enabling them to quickly identify issues and improve response times.
Simplicity: data visualization techniques gives the full picture of the scoped parameters and simplify the data by enabling decision makers to cherry-pick the relevant data they need and dive to detailed wherever is needed.
Easier Pattern Visualization: provides easier approaches to identify upcoming trends and patterns within datasets, and hence enable to take efficient decisions and prepare strategies in advance.
Team Involvement: increase the levels of
collaboration between departments and keep them on the same page to achieve strategic goals.
Unify Interpretation: produced charts and graphics have the same interpretation by all beneficial who use extracted information for decisions making and hence avoid any misleading.
5. Write a Python script to create a data frame for the following table:
Name Mobile_Sales TV_Sales
Ahmed 2540 2200
Omar 1370 1900
Ali 1320 2150
Ziad 2000 1850
Salwa 2100 1770
Lila 2150 2000
Answer:
In [ ]: import pandas as pd import numpy as np
import matplotlib.pyplot as plt
salesMen = ['Ahmed', 'Omar', 'Ali', 'Ziad', 'Salwa', 'Lila']
Mobile_Sales = [2540, 1370, 1320, 2000, 2100, 2150]
TV_Sales = [2200, 1900, 2150, 1850, 1770, 2000]
df = pd.DataFrame() df ['Name'] =salesMen
df ['Mobile_Sales'] = Mobile_Sales df['TV_Sales']=TV_Sales
df.set_index("Name",drop=True,inplace=True) In [13]: df
Figure 2-22. Bar plot of sales
Out[13]: Name Mobile_Sales TV_Sales Ahmed 2540 2200 Omar 1370 1900 Ali 1320 2150 Ziad 2000 1850 Salwa 2100 1770 Lila 2150 2000
For the created data frame in the previous question, do the following:
A. Create a bar plot of the sales volume.
Answer:
In [5]: df.plot.bar( figsize=(20, 10), rot=0).legend(bbox_to_
anchor=(1.1, 1)) plt.xlabel('Salesmen') plt.ylabel('Sales') plt.title('Sales Volume for two salesmen in \nJanuary and April 2017') plt.show()
See also Figure 2-22.
Figure 2-23. Pie chart of sales
B. Create a pie chart of item sales.
Answer:
In [6]: df.plot.pie(subplots=True) See also Figure 2-23.
C. Create a box plot of item sales.
Answer:
In [8]: df.plot.box() See also Figure 2-24.
D. Create an area plot of item sales.
Answer:
In [9]: df.plot.area(figsize=(6, 4)).legend(bbox_to_anchor=(1.3, 1))
See also Figure 2-25.
Figure 2-24. Box plot of sales
E. Create a stacked bar plot of item sales.
Answer:
In [11]: df.plot.bar(stacked=True, figsize=(20, 10)).legend (bbox_to_anchor=(1.1, 1))
See also Figure 2-26.
Figure 2-25. Area plot of sales
Figure 2-26. Stacked bar plot of sales