What state is the most market-friendly?
While exploring the farmer's market data, you wonder what patterns may show up if you aggregated to the state level. Are some states more market-friendly than other states? To investigate this, you group your data by state and get the log-transformed number of markets (log_markets
) and state populations (log_pop
).
markets_and_pop = (markets
.groupby('state', as_index = False)
.agg({
'name': lambda d: log(len(d)),
'state_pop': lambda d: log(d.iloc[0]) })
.rename(columns = {
'name': 'log_markets',
'state_pop': 'log_pop' }))
To visualize, you decide to use a regression plot to get an idea of the 'normal' relationship between market and population numbers and a text-scatter to quickly identify interesting outliers.
Este exercício faz parte do curso
Improving Your Data Visualizations in Python
Instruções do exercício
- Iterate over the rows of the
markets_and_pop
DataFrame. - Place annotations next to their scatter plot points.
- Reduce annotation text size to
10
points.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
g = sns.regplot(
"log_markets", "log_pop",
ci = False,
# Shrink scatter plot points
scatter_kws = {'s':2},
data = markets_and_pop)
# Iterate over the rows of the data
for _, row in markets_and_pop.____():
state, _, _, log_markets, log_pop = row
# Place annotation and reduce size for clarity
g.annotate(state, (____,____), ____ = ____)
plt.show()