Professional Documents
Culture Documents
Practice
In [1]:
Graph
In [2]:
x = [1,2,3,4,5]
[6 7 8 9 10]
In [3]:
plt.plot(x, y)
plt.xlabel('Tree')
plt.ylabel('Mountain')
plt.title('Graph')
lt h ()
In [4]:
Bar Chart
In [5]:
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Vertical Bar Chart')
plt.bar(x, y, width = 0.9, color = ['yellow', 'cyan'])
lt h ()
In [6]:
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Horizontal Bar Chart')
plt.barh(x, y, color = ['fuchsia', 'teal'])
lt h ()
Histogram
In [7]:
x = np.random.normal(180, 5, 200)
plt.hist(x)
plt.xlabel('x_axis')
plt.ylabel('y-axis')
plt.title('Histogram')
lt h ()
In [8]:
x = np.array([87,87,5,89,56,73,34,54,31,17,51,5,79,21])
bins = ([0,25,50,75,100])
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Histogram')
lt h ()
Scatter Plot
In [9]:
x = np.random.rand(10)
y = np.random.rand(10)
plt.scatter(x, y)
lt h ()
In [10]:
plt.scatter(x, y)
lt h ()
Pie Chart
In [11]:
plt.pie(values, labels = names, radius = 1.5, startangle = 90, shadow=True, explode = (0.05
# explode : specifies the fraction of the radius with which to offset each wedge, autopct :
plt.legend(title = 'Content of the Universe') # to add a list of explanation for each wedge
lt h ()
Curves
In [12]:
# parabola y = x^2
x = np.linspace(-100, 100, 50000) # generates linearly spaced vectors
y = x**2
In [13]:
plt.plot(x, y, x, z)
x = np.linspace(-2,2,41)
y = np.exp(-x**2) * np.cos(2*np.pi*x)
plt.plot(x, y, alpha = 0.4, label = 'Decaying Cosine', color = 'c', linestyle = 'dashed', l
marker = 'o', markersize = 5, markerfacecolor = 'g', markeredgecolor = 'b')
plt.ylim([-3 ,3])
plt.legend()
lt h ()
Pandas
In [15]:
i t d d
In [16]:
df d ib ()
Out[17]:
In [18]:
df i f ()
<class 'pandas.core.frame.DataFrame'>
In [19]:
df h d()
Out[19]:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare C
Braund,
0 1 0 3 Mr. Owen male 22.0 1 0 A/5 21171 7.2500
Harris
Cumings,
Mrs. John
Bradley
1 2 1 1 female 38.0 1 0 PC 17599 71.2833
(Florence
Briggs
Th...
Heikkinen,
STON/O2.
2 3 1 3 Miss. female 26.0 0 0 7.9250
3101282
Laina
Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803 53.1000
Heath
(Lily May
Peel)
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450 8.0500
Henry
In [20]:
df t il()
Out[20]:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cab
Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536 13.00 Na
Juozas
Graham,
Miss.
887 888 1 1 female 19.0 0 0 112053 30.00 B4
Margaret
Edith
Johnston,
Miss.
W./C.
888 889 0 3 Catherine female NaN 1 2 23.45 Na
6607
Helen
"Carrie"
Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369 30.00 C14
Howell
Dooley,
890 891 0 3 Mr. male 32.0 0 0 370376 7.75 Na
Patrick
In [21]:
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabi
Crosby,
Capt. WE/P
745 746 0 1 male 70.0 1 1 71.000 B2
Edward 5735
Gifford
Crosby,
WE/P
540 541 1 1 Miss. female 36.0 0 2 71.000 B2
5735
Harriet R
Harris,
W/C
219 220 0 2 Mr. male 30.0 0 0 10.500 Na
14208
Walter
Chaffee,
Mr. W.E.P.
92 93 0 1 male 46.0 1 0 61.175 E3
Herbert 5734
Fuller
Harknett,
Miss. W./C.
235 236 0 3 female NaN 0 0 7.550 Na
Alice 6609
Phoebe
In [ ]:
Assignment
In [22]:
df d ib ()
Out[23]:
df i f ()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 12 entries, 0 to 11
dtypes: int64(9)
In [25]:
df h d()
Out[25]:
In [26]:
df t il()
Out[26]:
1. Read Total profit of all months and show it using a line plot
Total profit data provided for each month. Generated line plot must include the following properties: –
In [27]:
# Method 1
# Reading a CSV file with specific columns
plt.plot(x, y)
plt.xlabel('Month Number')
plt.ylabel('Total Profit')
plt.title('Company profit per month')
lt h ()
In [28]:
# Method 2
df = pd.read_csv("company_sales_data.csv")
y = df ['total_profit']
x = df ['month_number']
plt.plot(x, y)
plt.xlabel('Month Number')
plt.ylabel('Total Number')
plt.xticks(x)
plt.yticks([100000, 200000, 300000, 400000, 500000])
plt.title('Company profit per month')
lt h ()
2. Get total profit of all months and show line plot with the following Style
properties
Generated line plot must include following Style properties: –
In [29]:
plt.plot(x, y, label = 'Profit Data', linestyle = 'dotted', color = 'r', marker = 'o', mark
plt.xlabel('Month Number')
plt.ylabel('Sold Units Number')
plt.xticks(x)
plt.yticks([100000, 200000, 300000, 400000, 500000])
plt.title('Company Sales data of last year')
plt.legend(loc = 'lower right')
lt h ()
3. Read all product sales data and show it using a multiline plot
Display the number of units sold per month for each product using multiline plots. (i.e., Separate Plotline for
each product ).
In [30]:
facecream = df['facecream']
facewash = df['facewash']
toothpaste = df['toothpaste']
bathingsoap = df['bathingsoap']
shampoo = df['shampoo']
moisturizer = df['moisturizer']
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.xticks(x)
plt.yticks([1000, 2000, 4000, 6000, 8000, 10000, 12000, 15000, 18000])
plt.legend()
plt.title('Sales data')
lt h ()
4. Read toothpaste sales data of each month and show it using a scatter
plot
Also, add a grid in the plot. gridline style should “–“.
In [31]:
tp = df['toothpaste']
plt.scatter(x, tp, label = 'Toothpaste sales data')
plt.legend()
plt.xlabel('Month Number')
plt.ylabel('Number of units sold')
plt.xticks(x)
plt.grid(linestyle = '--')
plt.title('Toothpaste sales data')
lt h ()
5. Read face cream and facewash product sales data and show it using the bar
chart
The bar chart should display the number of units sold per month for each product. Add a separate bar for each
product in the same chart.
In [32]:
fc = df['facecream']
fw = df['facewash']
plt.bar(x, fc, width = -0.25, label = 'Facecream sales data', align = 'edge') # bar goes fr
plt.bar(x, fw, width = 0.25, label = 'Facewash sales data', align = 'edge') # bar goes from
plt.legend()
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.xticks(x)
plt.grid(linestyle = '--')
plt.title('Facewash and facecream sales data')
lt h ()
6. Read sales data of bathing soap of all months and show it using a bar chart.
Save this plot to your hard disk
In [33]:
bs = df['bathingsoap'].tolist()
plt.bar(x, bs)
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.xticks(x)
plt.grid(linestyle = '--')
plt.title('Bathingsoap sales data')
plt.savefig('D:\CO327_ML_Lab\Lab_3')
lt h ()
7. Read the total profit of each month and show it using the histogram to see the
most common profit ranges
In [34]:
tp = df ['total_profit']
range = [150000, 175000, 200000, 225000, 250000, 300000, 350000]
In [35]:
plt.legend()
plt.title('Sales data\n\n\n')
lt h ()
9. Read Bathing soap facewash of all months and display it using the
Subplot
In [36]:
bs = df['bathingsoap']
fw = df['facewash']
# given the number of rows and columns, it returns a tuple (fig ,ax), giving a single figur
plt.xticks(x)
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
lt h ()
10. Read all product sales data and show it using the stack plot
In [37]:
plt.xlabel('Month Number')
plt.ylabel('Sales units in Number')
plt.xticks(x)
plt.legend(loc = 'upper left')
plt.title('All product sales data using stack plot')
lt h ()