Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Or is it Holiday Effect? | Visualizing Data
Analyzing and Visualizing Real-World Data

book
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

  1. 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.

  2. Initialize a for loop to display the dates next to the points. Iterate over the indexes of data using index as the dummy variable.

  3. Within the loop, use the .text() method of plt 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 a 0.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'.

Soluzione

# 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?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 5
# 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

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt