Or is it Holiday Effect?
Let's find out if holidays could be the cause. To do this, we will display the dates next to the points.
Compito
Swipe to start coding
-
Add a
hue
parameter to the.scatterplot()
function so that the points will be colored in accordance to the values in the'Holiday_Flag'
column. -
Initialize a
for
loop to display the dates next to the points. Iterate over the indexes ofdata
usingindex
as the dummy variable. -
Within the loop, use the
.text()
method ofplt
to display text on the plot. The method should have 4 parameters:- the first is responsible for the x-coordinate of the text, which should be the
index
value of the'Temperature'
column with a0.2
offset; - the second is responsible for the y-coordinate of text, which should be the
index
value of the'Weekly_Sales'
column; - the third is the text that should be displayed which should be the
index
value of the'Date'
column, transformed into a date; - the fourth is the
color
parameter, responsible for color of the text, which should be'black'
.
- the first is responsible for the x-coordinate of the text, which should be the
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Loading the library
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Reading the data
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/72be5dde-f3e6-4c40-8881-e1d97ae31287/shops_data3.csv')
df['Date'] = pd.to_datetime(df['Date'], dayfirst = True)
# Preparing the data
top_stores = [20, 4, 14, 13, 2]
data = df.loc[(df['Store'].isin(top_stores)) & (df.Temperature >= -10)
& (df.Temperature <= 10) & (df.Weekly_Sales >= 3000000)]
# Initializing the plot
sns.scatterplot(x = 'Temperature', y = 'Weekly_Sales', hue = 'Holiday_Flag', data = data)
# Add Date labels to points
for index in data.index:
plt.text(data.Temperature[index] + 0.2, data.Weekly_Sales[index],
data.Date[index].date(), color = 'black')
# Displaying the plot
plt.show()
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 4. Capitolo 5
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Loading the library
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Reading the data
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/72be5dde-f3e6-4c40-8881-e1d97ae31287/shops_data3.csv')
df['Date'] = pd.to_datetime(df['Date'], dayfirst = True)
# Preparing the data
top_stores = [20, 4, 14, 13, 2]
data = df.loc[(df['Store'].isin(top_stores)) & (df['Temperature'] >= -10)
& (df['Temperature']) & (df['Weekly_Sales'] >= 3000000)]
# Initializing the plot
sns.scatterplot(x = 'Temperature', y = 'Weekly_Sales', ___, data = data)
# Add Date labels to points
for index in data.___:
plt.text(___[index] + 0.2, data___[index],
data___[index].date(), color = 'black')
# Displaying the plot
plt.show()
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione