Challenge 15 Megathread

For any and all questions relating to challenge 15. :point_down:

For a tutorial on how to use Jupyter Notebook, we put together this video:

Still have questions? Read all the FAQs here.

Very easy to get the solution. The procedure is given below:

df['potential_harvest'] = df['Each_Seed_Produces'] * df['Seeds_Count']
plt.figure()
plt.bar(x = df['Vegetable'], height = df['potential_harvest'])
plt.title('Potential Harvest')
plt.show()
6 Likes

Here is an over-complicated approach with numpy

items=seeds[‘Vegetable’]
count=seeds[‘Seeds_Count’]
produced=seeds[‘Each_Seed_Produces’]
aCount =np.array(count)
aProduced= np.array(produced)

aHarvest = aCount*aProduced

plt.figure()
plt.bar(x=items, height = aHarvest)
plt.show()

1 Like

Seems straightforward

df['prod'] = df['Seeds_Count'] * df['Each_Seed_Produces']
plt.figure() #The Frame: We start our plot with a figure
plt.bar(x = df['Vegetable'], height = df['prod']) #The Body: Declaring the specific bar plot statment
plt.title("Vegetable x Production") #Stylistic Features: Adding the title
plt.show() #To show our plot, we need to end our plot with a plt.show()
1 Like

Wow this seems so straightforward! I feel a bit silly with my (now obviously) over-complicated approach. I tried to follow the example but now I understand it was unnecessary, as seeing what you’ve done I realise I can just put in the dataframe columns as the x and the height. Thanks!

2 Likes

Matplotlib - this is the first time I hear about it.
Pretty easy, after seeing the example.

Summary

Screenshot from 2021-03-02 21-32-37

Video Solution: https://youtu.be/pluxvBPd_6o

2 Likes

I had similar solution with matplotlib. My only pet peeve with the matplotlib bar charts is that it’s a pain to add data labels on top of the bars (you have to first find out the x-coordinates of the bars then use plt.annotate to plot them).

With other visualization packages like plotly, it’s just another simple argument added to the formula.

Here is the code using plotly express (you do have to install it first though):

!pip install plotly
import plotly.express as px
df['Harvest'] = df['Seeds_Count'] * df['Each_Seed_Produces']
fig = px.bar(df, y='Harvest', x='Vegetable', text='Harvest')
fig.show()

10 Likes

My code: I didn’t create a new column, just did the calculation in the function:

  plt.figure()
    plt.bar(x = df['Vegetable'], height = df['Seeds_Count'] * df['Each_Seed_Produces'])
    plt.show()
5 Likes

I found this one to be a bit easier than some past ones. Tried to use some different things like zip and a loop to make a new list of values for the table. I also figured out that you can use the information in the other cells by running them in order one after another; better late than never I suppose.

Summary
# Make lists from df
veg = list(df.Vegetable)
scnt = list(df.Seeds_Count)
esp = list(df.Each_Seed_Produces)
# New var for Seed_Count * Each_Seed_Produces
amnt = []
for v, c in zip(esp, scnt):
    tot = v * c
    amnt.append(tot)

plt.figure()
plt.title('Potential Harvest')
plt.bar(x = veg, height = amnt)
plt.show()

Great work! Thank you so much for sharing with us! I think Plotly is better suited for creating elaborate plots more efficiently than Matplotlib!!

1 Like

so why plt.bar rather than df.plot?

df[‘Yield’] = df[“Seeds_Count”]*df[“Each_Seed_Produces”]
df.plot(kind=‘bar’,x=‘Vegetable’,y=‘Yield’)

2 Likes

Explored other visualizations and their color features. It was fun~

series = df[“Seeds_Count”] * df[‘Each_Seed_Produces’]

series.plot.pie(
labels= df[‘Vegetable’],
colors=[“orange”, “pink”, “brown”, “purple”,“green”],
autopct="%.2f",
fontsize=20,
figsize=(6, 6),
title = “Potential Harvest”);

10 Likes

I want to create the most convoluted chart to represent this data, just for a laugh.
Here’s my attempt with a bubble chart:
I especially like that the colors are not the same as the real veg color :smiley:
Any other takers?

Code is:

import pandas as pd
import plotly.express as px
seeds = {
    'Vegetable' : ['Carrots', 'Tomatoes', 'Potatoes', 'Eggplant', 'Cucumbers'],
    'Seeds_Count' : [300,10,90,100,15],
    'Each_Seed_Produces': [1,140,10,5, 90]
}

df = pd.DataFrame(seeds)

df['potential_harvest'] = df.Seeds_Count * df.Each_Seed_Produces


fig = px.scatter(df, x="Vegetable", y="potential_harvest",
	         size="potential_harvest", color='Vegetable',
                 hover_name="Vegetable",  size_max=60, labels='potential_harvest', title='Seed Data')
fig.show()
8 Likes

This looks interesting! Thanks for sharing. Can you tell me what the ‘zip’ function does?

I don’t really understand their criteria to categorize a challenge as easy or hard. I’ve struggled to solve some of the easy ones, and found today’s “hard” challenge really easy to solve.

My solution:

potential_harvest = []

for count, numseeds in zip(seeds['Seeds_Count'], seeds['Each_Seed_Produces']):
    potential_harvest.append(count * numseeds)

plt.figure()
plt.bar(x = df['Vegetable'], height = potential_harvest)
plt.show()

I added conditional colors to highlight the max:

df['Total_Produce']=df['Seeds_Count']*df['Each_Seed_Produces']
plt.figure()
clrs=['magenta' if (x==max(df['Total_Produce'])) else 'silver' for x in df['Total_Produce']]
plt.bar(x=df['Vegetable'], height=df['Total_Produce'], color=clrs)
plt.show()

Screen Shot 2021-03-03 at 10.50.31

8 Likes

also had some fun with color names - using actual food names :crazy_face::

clrs=['tomato' if (x==max(df['Total_Produce'])) else 'plum' for x in df['Total_Produce']]
plt.bar(x=df['Vegetable'], height=df['Total_Produce'], color=clrs)

Screen Shot 2021-03-03 at 11.02.29

7 Likes