Data Visualizationyuo
Data Visualizationyuo
Triangle patterns:
def fibonacci(n):
a, b = 0, 1
a, b = b, a + b
return a
def numeric_triangle(rows):
numeric_triangle(rows)
Output:
12
123
1234
12345
Output:
Sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
Unique values in sepal length (cm): [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.4, 4.8, 4.3,
5.8, …]
5.0 10
5.1 9
6.3 7
Question 3 : On the Iris Data Set Show the addition of new columns, perform
filtering based on acolumn value and show the use of group by function.
import pandas as pd
Output :
Sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
petal area
Sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
petal area
Sepal length (cm) petal length (cm) petal width (cm) petal area
Sepal width (cm)
import pandas as pd
Output :
Sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
petal area
Sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
petal area
Sepal length (cm) petal length (cm) petal width (cm) petal area
Question 5: On the height - weight DataSet print the top 5, bottom 5, and
random rows. Group bythe height column and update the height of students
in the group having no. ofstudents greater than 70.
import pandas as pd
height_groups = df.groupby(“height”).size().reset_index(name=”student
count”)
Output:
Height Weight
0 150.0 50.0
1 160.2 65.5
2 155.5 58.3
3 170.0 75.0
4 165.1 68.4
Bottom 5 Rows
Height Weight
95 165.0 68.0
96 155.8 59.0
97 172.5 78.3
98 167.0 70.2
99 160.0 65.0
Height Weight
42 172.0 77.5
67 158.5 60.8
23 161.0 64.2
89 153.0 55.1
12 170.0 74.0
0 150.0 10
1 155.5 25
2 160.0 80
3 165.0 95
4 170.0 60
Height Weight
0 150.0 50.0
2 155.5 58.3
3 170.0 75.0
Question 6: Show the use of shape, size, type, dtypes, columns and info
properties of a DataFrame.
import pandas as pd
data = {
df = pd.DataFrame(data)
print("\nDataFrame Info:")
df.info()
Output:
size of dataframe: 20
name object
age int64
salary int64
department object
dtype: object
columns in dataframe
summary info
dataframe info:
<CLASS ‘PANDAS.CORE.FRAME.DATAFRAME’>
rangeindex: 5 entries, 0 to 4
Question 7: Draw a line chart exploring its styling properties like figsize,
xlabel, ylabel, title,subtitle, color, marker, linestyle, linewidth.
years = [2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024]
sales = [10, 15, 12, 20, 18, 25, 30, 35, 40, 38] # sales in millions
plt.plot(years, sales,
plt.show()
Output :
Question 8: Draw a scatter plot exploring its properties like color, alpha, size,
labels.
budget = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
sales = [15, 25, 35, 30, 55, 60, 70, 85, 90, 100]
plt.scatter(budget, sales,
Output:
Question 9: Draw a bar graph with vertical and horizontal orientations.
Explore color, width, heightand other properties.
products = [“product a”, “product b”, “product c”, “product d”, “product e”]
axs[0].bar(products, sales,
axs[1].barh(products, sales,
plt.tight_layout()
plt.show()
Output :
QQuestion10: Draw a histogram exploring properties like Bins, colors, alpha,
labels, legend and fontsize.
import numpy as np
plt.hist(data,
plt.legend(fontsize=12)
plt.show()
Output :
Question11: Draw a pie chart exploring its properties like labels, colors,
radius, explode, shadow,autopct.
import matplotlib.pyplot as plt
plt.pie(sizes,
plt.show()
Output:
Question 12 : Draw line chart, Scatter plot, histogram on the iris data set
with styling.
import pandas as pd
df = pd.read_csv("iris.csv")
plt.figure(figsize=(10, 5))
plt.plot(df['sepal_length'],
color='blue',
marker='o',
linestyle='--',
linewidth=2,
label='Sepal Length')
plt.xlabel("Index", fontsize=12)
plt.legend()
plt.tight_layout()
plt.show()
plt.figure(figsize=(8, 5))
plt.scatter(df['petal_length'],
df['petal_width'],
color='green',
alpha=0.6,
s=80,
edgecolor='black')
plt.tight_layout()
plt.show()
plt.figure(figsize=(8, 5))
plt.hist(df['sepal_width'],
bins=12
color='purple',
alpha=0.7,
edgecolor='black',
label='Sepal Width')
plt.ylabel("Frequency", fontsize=12)
plt.legend()
plt.show()
Output :
Question 13 : Draw boxplot with the properties like facecolor, colors,
capprops like color andlinewidth. Show how the box plot can be used to
detect outliers. Add two outlier rowsmanually
import pandas as pd
df = pd.read_csv("iris.csv")
outliers = pd.DataFrame({
})
box = plt.boxplot(df['sepal_length'],
capprops=dict(color='darkgreen', linewidth=2),
medianprops=dict(color='black', linewidth=2)
plt.tight_layout()
plt.show()
Output: