matplotlib fivethirtyeight style graph

Solutions on MaxInterview for matplotlib fivethirtyeight style graph by the best coders in the world

showing results for - "matplotlib fivethirtyeight style graph"
Hans
11 Jul 2017
1positive_white = white_corr >= 0
2color_map_white = positive_white.map({True:'#33A1C9', False:'#ffae42'})
3
4style.use('fivethirtyeight')
5fig, ax = plt.subplots(figsize=(9, 5))
6ax.barh(white_corr.index, white_corr, left=2, height=0.5,
7        color=color_map_white)
8#ax.barh(red_corr.index, red_corr, height=0.5, left=-0.1)
9
10ax.grid(b=False)
11ax.set_yticklabels([])
12ax.set_xticklabels([])
13
14x_coords = {'Alcohol': 0.82, 'Sulphates': 0.77, 'pH': 0.91,
15            'Density': 0.80, 'Total Sulfur Dioxide': 0.59,
16            'Free Sulfur Dioxide': 0.6, 'Chlorides': 0.77,
17            'Residual Sugar': 0.67, 'Citric Acid': 0.76,
18            'Volatile Acidity': 0.67, 'Fixed Acidity': 0.71}
19y_coord = 9.8
20
21for y_label, x_coord in x_coords.items():
22    ax.text(x_coord, y_coord, y_label)
23    y_coord -= 1
24    
25ax.axvline(0.5, c='grey', alpha=0.1, linewidth=1,
26           ymin=0.1, ymax=0.9)
27ax.axvline(1.45, c='grey', alpha=0.1, linewidth=1,
28           ymin=0.1, ymax=0.9)
29
30ax.axhline(-1, color='grey', linewidth=1, alpha=0.5,
31          xmin=0.01, xmax=0.32)
32ax.text(-0.7, -1.7, '-0.5'+ ' '*31 + '+0.5',
33        color='grey', alpha=0.5)
34
35ax.axhline(-1, color='grey', linewidth=1, alpha=0.5,
36           xmin=0.67, xmax=0.98)
37ax.text(1.43, -1.7, '-0.5'+ ' '*31 + '+0.5',
38        color='grey', alpha=0.5)
39
40ax.axhline(11, color='grey', linewidth=1, alpha=0.5,
41          xmin=0.01, xmax=0.32)
42ax.text(-0.33, 11.2, 'RED WINE', weight='bold')
43
44ax.axhline(11, color='grey', linewidth=1, alpha=0.5,
45          xmin=0.67, xmax=0.98)
46ax.text(1.75, 11.2, 'WHITE WINE', weight='bold')
47
48ax.text(-0.7, -2.9, '©DATAQUEST' + ' '*92 + 'Source: P. Cortez et al.',
49        color = '#f0f0f0', backgroundcolor = '#4d4d4d',
50       size=12)
51
52ax.text(-0.7, 13.5,
53        'Wine Quality Most Strongly Correlated With Alcohol Level',
54        size=17, weight='bold')
55ax.text(-0.7, 12.7,
56        'Correlation values between wine quality and wine properties (alcohol, pH, etc.)')
57
58positive_red = red_corr >= 0
59color_map_red = positive_red.map({True:'#33A1C9',False:'#ffae42'})
60
61ax.barh(red_corr.index, red_corr,height=0.5, left=-0.1, color=color_map_red)
62
63plt.show()