Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit de478bf

Browse filesBrowse files
committed
Add more graph examples
1 parent 057fa69 commit de478bf
Copy full SHA for de478bf

File tree

Expand file treeCollapse file tree

8 files changed

+136
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+136
-0
lines changed
Open diff view settings
Collapse file
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# bar_chart_title.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def bar_chart(numbers, labels, pos):
6+
plt.bar(pos, [4, 5, 6, 3], color='green')
7+
plt.bar(pos, numbers, color='blue')
8+
plt.xticks(ticks=pos, labels=labels)
9+
plt.title('Gas Used in Various Vehicles')
10+
plt.xlabel('Vehicle Types')
11+
plt.ylabel('Number of Vehicles')
12+
plt.show()
13+
14+
if __name__ == '__main__':
15+
numbers = [2, 1, 4, 6]
16+
labels = ['Electric', 'Solar', 'Diesel', 'Unleaded']
17+
pos = list(range(4))
18+
bar_chart(numbers, labels, pos)
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# bar_chart_labels.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def bar_chart(numbers, labels, pos):
6+
plt.bar(pos, numbers, color='blue')
7+
plt.xticks(ticks=pos, labels=labels)
8+
plt.xlabel('Vehicle Types')
9+
plt.ylabel('Number of Vehicles')
10+
plt.show()
11+
12+
if __name__ == '__main__':
13+
numbers = [2, 1, 4, 6]
14+
labels = ['Electric', 'Solar', 'Diesel', 'Unleaded']
15+
pos = list(range(4))
16+
bar_chart(numbers, labels, pos)
Collapse file
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# bar_chart_legend.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def bar_chart(numbers, labels, pos):
6+
plt.bar(pos, [4, 5, 6, 3], color='green')
7+
plt.bar(pos, numbers, color='blue')
8+
plt.xticks(ticks=pos, labels=labels)
9+
plt.xlabel('Vehicle Types')
10+
plt.ylabel('Number of Vehicles')
11+
plt.title('Gas Used in Various Vehicles')
12+
plt.legend(['First Label', 'Second Label'],
13+
loc='upper left')
14+
plt.show()
15+
16+
if __name__ == '__main__':
17+
numbers = [2, 1, 4, 6]
18+
labels = ['Electric', 'Solar', 'Diesel', 'Unleaded']
19+
pos = list(range(4))
20+
bar_chart(numbers, labels, pos)
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# multiple_figures.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def line_plot(numbers, numbers2):
6+
first_plot = plt.figure(1)
7+
plt.plot(numbers)
8+
9+
second_plot = plt.figure(2)
10+
plt.plot(numbers2)
11+
plt.show()
12+
13+
if __name__ == '__main__':
14+
numbers = [2, 4, 1, 6]
15+
more_numbers = [5, 1, 10, 3]
16+
line_plot(numbers, more_numbers)
Collapse file

‎chapter40_graphs/multiple_plots.py‎

Copy file name to clipboard
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# multiple_plots.py
2+
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
def multiple_plots():
7+
# Some example data to display
8+
x = np.linspace(0, 2 * np.pi, 400)
9+
y = np.sin(x ** 2)
10+
11+
fig, axs = plt.subplots(2)
12+
fig.suptitle('Vertically stacked subplots')
13+
axs[0].plot(x, y)
14+
axs[1].plot(x, -y)
15+
plt.show()
16+
17+
if __name__ == '__main__':
18+
multiple_plots()
Collapse file
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# multiple_plots2.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def multiple_plots():
6+
numbers = [2, 4, 1, 6]
7+
more_numbers = [5, 1, 10, 3]
8+
fig, axs = plt.subplots(2)
9+
fig.suptitle('Vertically stacked subplots')
10+
axs[0].plot(numbers)
11+
axs[1].plot(more_numbers)
12+
plt.show()
13+
14+
if __name__ == '__main__':
15+
multiple_plots()
Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# pie_chart.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def pie_chart():
6+
numbers = [40, 35, 15, 10]
7+
labels = ['Python', 'Ruby', 'C++', 'PHP']
8+
# Explode the first slice (Python)
9+
explode = (0.1, 0, 0, 0)
10+
11+
fig1, ax1 = plt.subplots()
12+
ax1.pie(numbers, explode=explode, labels=labels,
13+
shadow=True, startangle=90,
14+
autopct='%1.1f%%')
15+
ax1.axis('equal')
16+
plt.show()
17+
18+
if __name__ == '__main__':
19+
pie_chart()
Collapse file
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# pie_chart_plain.py
2+
3+
import matplotlib.pyplot as plt
4+
5+
def pie_chart():
6+
numbers = [40, 35, 15, 10]
7+
labels = ['Python', 'Ruby', 'C++', 'PHP']
8+
9+
fig1, ax1 = plt.subplots()
10+
ax1.pie(numbers, labels=labels)
11+
plt.show()
12+
13+
if __name__ == '__main__':
14+
pie_chart()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.