## 2019年6月30日 星期日

### [ Python 文章收集 ] Bar Charts in Matplotlib

Source From Here
Preface
Bar charts are used to display values associated with categorical data. The plt.bar function, however, takes a list of positions and values, the labels for x are then provided by plt.xticks()
1. import matplotlib.pyplot as plt
2. %matplotlib inline
3. plt.style.use('ggplot')
4.
5. x = ['Nuclear''Hydro''Gas''Oil''Coal''Biofuel']
6. energy = [561522248]
7.
8. x_pos = [i for i, _ in enumerate(x)]
9.
10. plt.bar(x_pos, energy, color='green')
11. plt.xlabel("Energy Source")
12. plt.ylabel("Energy Output (GJ)")
13. plt.title("Energy output from various fuel sources")
14.
15. plt.xticks(x_pos, x)
16.
17. plt.show()

Error bars
We can extend the above with error bars as follows:
1. x = ['Nuclear''Hydro''Gas''Oil''Coal''Biofuel']
2. energy = [561522248]
3. variance = [127423]
4.
5. x_pos = [i for i, _ in enumerate(x)]
6.
7. plt.bar(x_pos, energy, color='green', yerr=variance)
8. plt.xlabel("Energy Source")
9. plt.ylabel("Energy Output (GJ)")
10. plt.title("Energy output from various fuel sources")
11.
12. plt.xticks(x_pos, x)
13.
14. plt.show()

Horizontal Bar Chart
We can show the exact same chart horizontally using plt.barh()
1. x = ['Nuclear''Hydro''Gas''Oil''Coal''Biofuel']
2. energy = [561522248]
3. variance = [127423]
4.
5. x_pos = [i for i, _ in enumerate(x)]
6.
7. plt.barh(x_pos, energy, color='green', xerr=variance)
8. plt.ylabel("Energy Source")
9. plt.xlabel("Energy Output (GJ)")
10. plt.title("Energy output from various fuel sources")
11.
12. plt.yticks(x_pos, x)
13.
14. plt.show()

Bar Chart with Multiple X’s
To include multiple X values on the same chart, we can reduce the width of the bars and then place the indices one bar’s width further from the y axis:
1. import numpy as np
2.
3. N = 5
4. men_means = (2035303527)
5. women_means = (2532342025)
6.
7. ind = np.arange(N)
8. width = 0.35
9. plt.bar(ind, men_means, width, label='Men')
10. plt.bar(ind + width, women_means, width,
11.     label='Women')
12.
13. plt.ylabel('Scores')
14. plt.title('Scores by group and gender')
15.
16. plt.xticks(ind + width / 2, ('G1''G2''G3''G4''G5'))
17. plt.legend(loc='best')
18. plt.show()

Stacked Bar Chart
With stacked bar charts we need to provide the parameter bottom, this informs matplotlib where the bar should start from, so we will add up the values below:
1. countries = ['USA''GB''China''Russia''Germany']
2. bronzes = np.array([3817261915])
3. silvers = np.array([3723181810])
4. golds = np.array([4627261917])
5. ind = [x for x, _ in enumerate(countries)]
6.
7. plt.bar(ind, golds, width=0.8, label='golds', color='gold', bottom=silvers+bronzes)
8. plt.bar(ind, silvers, width=0.8, label='silvers', color='silver', bottom=bronzes)
9. plt.bar(ind, bronzes, width=0.8, label='bronzes', color='#CD853F')
10.
11. plt.xticks(ind, countries)
12. plt.ylabel("Medals")
13. plt.xlabel("Countries")
14. plt.legend(loc="upper right")
15. plt.title("2012 Olympics Top Scorers")
16.
17. plt.show()

If we wanted to view the same bar charts but as a proportion of the total medals won by that country, we can do the following:
1. total = bronzes + silvers + golds
2. proportion_bronzes = np.true_divide(bronzes, total) * 100
3. proportion_silvers = np.true_divide(silvers, total) * 100
4. proportion_golds = np.true_divide(golds, total) * 100
5.
6. plt.bar(ind, proportion_golds, width=0.8, label='golds', color='gold', bottom=proportion_bronzes+proportion_silvers)
7. plt.bar(ind, proportion_silvers, width=0.8, label='silvers', color='silver', bottom=proportion_bronzes)
8. plt.bar(ind, proportion_bronzes, width=0.8, label='bronzes', color='#CD853F')
9.
10. plt.xticks(ind, countries)
11. plt.ylabel("Medals")
12. plt.xlabel("Countries")
13. plt.title("2012 Olympics Top Scorers' Medals by Proportion")
14. plt.ylim=1.0
15.
16. # rotate axis labels
17. plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
18.
19. plt.show()

### [LeetCode] Medium - 227. Basic Calculator II (G,M,A,F)

Source From  Here Question Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-nega...