Tuesday, November 26, 2019

Learning Datascience Day 6

In [1]:
import numpy as pd
import pandas as pd
In [4]:
import matplotlib as plt
import seaborn as sns
In [6]:
days=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature=[36.4,37,39,39.4,45,42.6,33.5,43,42.5,45,46,46.7,41.5,43.6,41.7]
sns.lineplot(x=days,y=temperature)
Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43c04710>
In [7]:
#above is the line plot by saeborn
In [11]:
days=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
temperature=[36.4,37,39,39.4,45,42.6,33.5,43,42.5,45,46,46.7,41.5,43.6,41.7]
temp_df=pd.DataFrame({"days":days,"temperature":temperature})
sns.lineplot(x="days",y="temperature",data=temp_df)
Out[11]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f42c0ae80>
In [12]:
#above is plotting in dataframe
In [13]:
tips=sns.load_dataset('tips')
In [14]:
print(tips)
     total_bill   tip     sex smoker   day    time  size
0         16.99  1.01  Female     No   Sun  Dinner     2
1         10.34  1.66    Male     No   Sun  Dinner     3
2         21.01  3.50    Male     No   Sun  Dinner     3
3         23.68  3.31    Male     No   Sun  Dinner     2
4         24.59  3.61  Female     No   Sun  Dinner     4
5         25.29  4.71    Male     No   Sun  Dinner     4
6          8.77  2.00    Male     No   Sun  Dinner     2
7         26.88  3.12    Male     No   Sun  Dinner     4
8         15.04  1.96    Male     No   Sun  Dinner     2
9         14.78  3.23    Male     No   Sun  Dinner     2
10        10.27  1.71    Male     No   Sun  Dinner     2
11        35.26  5.00  Female     No   Sun  Dinner     4
12        15.42  1.57    Male     No   Sun  Dinner     2
13        18.43  3.00    Male     No   Sun  Dinner     4
14        14.83  3.02  Female     No   Sun  Dinner     2
15        21.58  3.92    Male     No   Sun  Dinner     2
16        10.33  1.67  Female     No   Sun  Dinner     3
17        16.29  3.71    Male     No   Sun  Dinner     3
18        16.97  3.50  Female     No   Sun  Dinner     3
19        20.65  3.35    Male     No   Sat  Dinner     3
20        17.92  4.08    Male     No   Sat  Dinner     2
21        20.29  2.75  Female     No   Sat  Dinner     2
22        15.77  2.23  Female     No   Sat  Dinner     2
23        39.42  7.58    Male     No   Sat  Dinner     4
24        19.82  3.18    Male     No   Sat  Dinner     2
25        17.81  2.34    Male     No   Sat  Dinner     4
26        13.37  2.00    Male     No   Sat  Dinner     2
27        12.69  2.00    Male     No   Sat  Dinner     2
28        21.70  4.30    Male     No   Sat  Dinner     2
29        19.65  3.00  Female     No   Sat  Dinner     2
..          ...   ...     ...    ...   ...     ...   ...
214       28.17  6.50  Female    Yes   Sat  Dinner     3
215       12.90  1.10  Female    Yes   Sat  Dinner     2
216       28.15  3.00    Male    Yes   Sat  Dinner     5
217       11.59  1.50    Male    Yes   Sat  Dinner     2
218        7.74  1.44    Male    Yes   Sat  Dinner     2
219       30.14  3.09  Female    Yes   Sat  Dinner     4
220       12.16  2.20    Male    Yes   Fri   Lunch     2
221       13.42  3.48  Female    Yes   Fri   Lunch     2
222        8.58  1.92    Male    Yes   Fri   Lunch     1
223       15.98  3.00  Female     No   Fri   Lunch     3
224       13.42  1.58    Male    Yes   Fri   Lunch     2
225       16.27  2.50  Female    Yes   Fri   Lunch     2
226       10.09  2.00  Female    Yes   Fri   Lunch     2
227       20.45  3.00    Male     No   Sat  Dinner     4
228       13.28  2.72    Male     No   Sat  Dinner     2
229       22.12  2.88  Female    Yes   Sat  Dinner     2
230       24.01  2.00    Male    Yes   Sat  Dinner     4
231       15.69  3.00    Male    Yes   Sat  Dinner     3
232       11.61  3.39    Male     No   Sat  Dinner     2
233       10.77  1.47    Male     No   Sat  Dinner     2
234       15.53  3.00    Male    Yes   Sat  Dinner     2
235       10.07  1.25    Male     No   Sat  Dinner     2
236       12.60  1.00    Male    Yes   Sat  Dinner     2
237       32.83  1.17    Male    Yes   Sat  Dinner     2
238       35.83  4.67  Female     No   Sat  Dinner     3
239       29.03  5.92    Male     No   Sat  Dinner     3
240       27.18  2.00  Female    Yes   Sat  Dinner     2
241       22.67  2.00    Male    Yes   Sat  Dinner     2
242       17.82  1.75    Male     No   Sat  Dinner     2
243       18.78  3.00  Female     No  Thur  Dinner     2

[244 rows x 7 columns]
In [15]:
tips.head()
Out[15]:
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
In [16]:
tips.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
total_bill    244 non-null float64
tip           244 non-null float64
sex           244 non-null category
smoker        244 non-null category
day           244 non-null category
time          244 non-null category
size          244 non-null int64
dtypes: category(4), float64(2), int64(1)
memory usage: 7.2 KB
In [17]:
#.info() will all the information regarding dataframe
In [18]:
tips['total_bill']
Out[18]:
0      16.99
1      10.34
2      21.01
3      23.68
4      24.59
5      25.29
6       8.77
7      26.88
8      15.04
9      14.78
10     10.27
11     35.26
12     15.42
13     18.43
14     14.83
15     21.58
16     10.33
17     16.29
18     16.97
19     20.65
20     17.92
21     20.29
22     15.77
23     39.42
24     19.82
25     17.81
26     13.37
27     12.69
28     21.70
29     19.65
       ...  
214    28.17
215    12.90
216    28.15
217    11.59
218     7.74
219    30.14
220    12.16
221    13.42
222     8.58
223    15.98
224    13.42
225    16.27
226    10.09
227    20.45
228    13.28
229    22.12
230    24.01
231    15.69
232    11.61
233    10.77
234    15.53
235    10.07
236    12.60
237    32.83
238    35.83
239    29.03
240    27.18
241    22.67
242    17.82
243    18.78
Name: total_bill, Length: 244, dtype: float64
In [19]:
#you can print any column as seen in the above example
In [20]:
tips['total_bill'].max()
Out[20]:
50.81
In [21]:
#finding the maximum bill as above
In [22]:
tips[tips['total_bill']==50.81]
Out[22]:
total_billtipsexsmokerdaytimesize
17050.8110.0MaleYesSatDinner3
In [23]:
tips['time'].value_counts()
Out[23]:
Dinner    176
Lunch      68
Name: time, dtype: int64
In [24]:
tips['size'].value_counts()
Out[24]:
2    156
3     38
4     37
5      5
6      4
1      4
Name: size, dtype: int64
In [25]:
#now we will do seaborn plotting using line
In [26]:
sns.lineplot(x='total_bill',y='tip',data=tips)
Out[26]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43db0d68>
In [27]:
sns.lineplot(x='size',y='tip',data=tips)
Out[27]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43ddc978>
In [28]:
#in the above 2 examples data=tips is the dataframe used for plotting the line graph
In [29]:
sns.lineplot(x='size',y='total_bill',data=tips)
Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43e785c0>
In [30]:
sns.lineplot(x='size',y='tip',data=tips,hue='time')
Out[30]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43ebfdd8>
In [31]:
#hue is an attribute of seaborn line plot. so inthe above example, it divivdes it to lunch and dinner
In [32]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time')
Out[32]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43f6d828>
In [33]:
#style makes one line different. like in the above example
In [34]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette='Accent')
Out[34]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f43fbc828>
In [35]:
#palette changes the colour, the following values can be used instead of accent with palette Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r
In [37]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette='Accent')
Out[37]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f442d2780>
In [38]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'])
Out[38]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f4434cbe0>
In [39]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'],hue_order=['Dinner','Lunch'])
Out[39]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f4435f860>
In [40]:
#hue_order=['Dinner','Lunch']) commands let you define if its luch or diner or car or anything else
In [41]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'], dashes=False)
Out[41]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f440813c8>
In [42]:
#to remove dashes in the line, we use the above command dashes=False
In [43]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'],markers=['o','^'])
Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f443b2128>
In [44]:
#in the above example we put markers
In [45]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'],markers=['o','^'],legend=False)
Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x17f44517d68>
In [46]:
#in the above example we removed the legends
In [47]:
sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'],markers=['o','^'])
plt.title('Line Plot')
plt.xlabel("Size")
plt.ylabel("Tip")
plt.show()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-76515a146d0b> in <module>
      1 sns.lineplot(x='size',y='tip',data=tips,hue='time', style='time',palette=['red','blue'],markers=['o','^'])
----> 2 plt.title('Line Plot')
      3 plt.xlabel("Size")
      4 plt.ylabel("Tip")
      5 plt.show()

AttributeError: module 'matplotlib' has no attribute 'title'
In [ ]:
 

No comments:

Post a Comment

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...