<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Auto MPG Data
Step1:  2D Binning
Default behavior for 2d binning is to bin the dimensions provided, then count the rows that fall into each bin. This is visualizing how the source data represents all possible combinations of mpg and displacement.
Step2:  Binning and Aggregating Values
For each x and y bin, the stat is used on values. After the binning operation, the aggregated values are then binned a second time so that a discrete color can be mapped to the aggregated value.
Step3:  Specifying the Number of Bins
Step4:  Mixing binning and non-binned data
Step5:  The Size of the Glyph Can be Altered
Step6:  Applying a Custom Palette
The number of discrete colors in the palette determines the number of bins used for applying colors.
Example with 6 Colors
Step7:  Example with 9 Colors
Step8:  Viewing Color Bins in Legend
Step9:  Build Fruit Data
Step10:  Without Dimension Binning
Each x and y value are treated as the coordinates of a bin. The values column is then binned and assigned to the color attribute, so discrete colors can be assigned.
Step11:  Unemployment Data
Prepare the Data
Current data is in a pivoted form, which is difficult to use when defining plots by specifying columns. We will use the pandas melt function to de-pivot the columns with numerical data we want to use, while keeping the categorical data.
Step12:  De-pivoted Data
We now have the 3 dimensions that we want to map to attributes of the plot.
<ASSISTANT_TASK:>
Python Code:
df.head()
Explanation: Auto MPG Data
End of explanation
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'))
show(hm)
Explanation: 2D Binning
Default behavior for 2d binning is to bin the dimensions provided, then count the rows that fall into each bin. This is visualizing how the source data represents all possible combinations of mpg and displacement.
End of explanation
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), values='cyl', stat='mean')
show(hm)
Explanation: Binning and Aggregating Values
For each x and y bin, the stat is used on values. After the binning operation, the aggregated values are then binned a second time so that a discrete color can be mapped to the aggregated value.
End of explanation
hm = HeatMap(df, x=bins('mpg'), y=bins('displ', bin_count=15), 
             values='cyl', stat='mean')
show(hm)
Explanation: Specifying the Number of Bins
End of explanation
hm = HeatMap(df, x=bins('mpg'), y='cyl', values='displ', stat='mean')
show(hm)
Explanation: Mixing binning and non-binned data
End of explanation
hm = HeatMap(df, y=bins('displ'), x=bins('mpg'), values='cyl', stat='mean',
             spacing_ratio=0.9)
show(hm)
Explanation: The Size of the Glyph Can be Altered
End of explanation
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
             palette=RdYlGn6)
show(hm)
Explanation: Applying a Custom Palette
The number of discrete colors in the palette determines the number of bins used for applying colors.
Example with 6 Colors
End of explanation
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), stat='mean', values='cyl',
             palette=RdYlGn9)
show(hm)
Explanation: Example with 9 Colors
End of explanation
hm = HeatMap(df, x=bins('mpg'), y=bins('displ'), values='cyl',
             stat='mean', legend='top_right')
show(hm)
Explanation: Viewing Color Bins in Legend
End of explanation
fruits = {'fruit': ['apples', 'apples', 'apples', 'apples', 'apples',
                    'pears', 'pears', 'pears', 'pears', 'pears',
                    'bananas', 'bananas', 'bananas', 'bananas', 'bananas'],
          'fruit_count': [4, 5, 8, 12, 4, 6, 5, 4, 8, 7, 1, 2, 4, 8, 12],
          'year': [2009, 2010, 2011, 2012, 2013, 2009, 2010, 2011, 2012, 2013, 2009, 2010,
                   2011, 2012, 2013]}
fruits['year'] = [str(yr) for yr in fruits['year']]
fruits_df = pd.DataFrame(fruits)
fruits_df.head()
Explanation: Build Fruit Data
End of explanation
hm = HeatMap(fruits, y='year', x='fruit', values='fruit_count', stat=None)
show(hm)
Explanation: Without Dimension Binning
Each x and y value are treated as the coordinates of a bin. The values column is then binned and assigned to the color attribute, so discrete colors can be assigned.
End of explanation
unempl_data = data.copy()
unempl_data.head()
# Remove the annual column if we don't want to show the total
del unempl_data['Annual']
# Convert numerical year to strings
unempl_data['Year'] = unempl_data['Year'].astype(str)
# de-pivot all columns, except for Year, into two columns.
# One column will have the values and the second will have the labels
unempl = pd.melt(unempl_data, var_name='Month', value_name='Unemployment', id_vars=['Year'])
Explanation: Unemployment Data
Prepare the Data
Current data is in a pivoted form, which is difficult to use when defining plots by specifying columns. We will use the pandas melt function to de-pivot the columns with numerical data we want to use, while keeping the categorical data.
End of explanation
unempl.head()
hm = HeatMap(unempl, x='Year', y='Month', values='Unemployment', stat=None,
             sort_dim={'x': False}, width=1000)
show(hm)
Explanation: De-pivoted Data
We now have the 3 dimensions that we want to map to attributes of the plot.
End of explanation
<END_TASK>