Cap 793
Cap 793
ANS 1-
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
print(df)
2- . Line Plot:
- Create a line plot to display the sales trends of Product A, Product B, and Product C over the
months using Matplotlib or Seaborn.
ANS 2-
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
plt.figure(figsize=(10,6))
plt.plot(df['Month'], df['Product_A_Sales'], label='Product A')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.show()
3 - . Bar Plot:
- Create a bar plot to compare the monthly sales of all three products.
ANS 3-
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
plt.figure(figsize=(10,6))
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.show()
Q4 - . Pie Chart - Create a pie chart to show the proportion of total annual sales contributed by
each product.
ANS 4 –
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
total_product_a_sales = df['Product_A_Sales'].sum()
total_product_b_sales = df['Product_B_Sales'].sum()
total_product_c_sales = df['Product_C_Sales'].sum()
plt.figure(figsize=(6,6))
plt.axis('equal')
plt.show()
Part 2: Advanced Data Visualization
ANS-1
import pandas as pd
import numpy as np
data = {
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500]
df = pd.DataFrame(data)
plt.figure(figsize=(8,6))
plt.scatter(df['Product_A_Sales'], df['Product_B_Sales'])
plt.title('Relationship between Product A Sales and Product B Sales')
plt.xlabel('Product A Sales')
plt.ylabel('Product B Sales')
z = np.polyfit(df['Product_A_Sales'], df['Product_B_Sales'], 1)
p = np.poly1d(z)
plt.plot(df['Product_A_Sales'],p(df['Product_A_Sales']),"r--")
plt.show()
Q 2- . Histogram:
ANS 2
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
plt.figure(figsize=(8,6))
plt.xlabel('Total Sales')
plt.ylabel('Frequency')
plt.show()
Q 3- . Box Plot:
- Create a box plot to visualize the distribution of sales for Product A, Product B, and Product C.
ANS 3-
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
plt.figure(figsize=(8,6))
plt.xlabel('Product')
plt.ylabel('Sales')
plt.show()
Q 4 -. Heatmap:
- Create a heatmap to show the correlation between the sales of Product A, Product B, and
Product C.
ANS 4-
import pandas as pd
import numpy as np
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
plt.figure(figsize=(8,6))
plt.show()
Part 3: Customization and Presentation
- Customize the plots with appropriate colors, markers, and styles to make them visually
appealing and informative.
ANS 1-
import pandas as pd
import numpy as np
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
plt.figure(figsize=(10,6))
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
max_sales_a = df['Product_A_Sales'].max()
max_sales_b = df['Product_B_Sales'].max()
max_sales_c = df['Product_C_Sales'].max()
plt.show()
plt.figure(figsize=(8,6))
plt.xlabel('Product')
plt.ylabel('Sales')
plt.show()
plt.figure(figsize=(8,6))
plt.show()
Q 2- . Subplots:
- Create a single figure with multiple subplots to present the data in a cohesive manner.
- Ensure the figure has a main title and each subplot is clearly labelled
ANS 2-
import pandas as pd
data = {
'Month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'],
'Product_A_Sales': [5000, 6000, 7000, 8000, 7500, 8200, 7800, 8400, 8200, 8600, 8800, 9000],
'Product_B_Sales': [7000, 6500, 7200, 6800, 7000, 7300, 7100, 7600, 7200, 7300, 7400, 7500],
'Product_C_Sales': [8000, 7000, 6800, 7500, 8200, 7900, 8500, 8100, 7900, 7800, 8000, 8300]
df = pd.DataFrame(data)
axs[0].set_xlabel('Month')
axs[0].set_ylabel('Sales')
axs[0].legend()
axs[1].plot(df['Month'], df['Total_Sales'])
axs[1].set_xlabel('Month')
axs[1].set_ylabel('Total Sales')
axs[2].plot(df['Month'], df['Average_Sales'])
axs[2].set_xlabel('Month')
axs[2].set_ylabel('Average Sales')