import matplotlib.pyplot as plt
## Data for the pie chart
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
## Create a figure and a set of subplots
fig, ax = plt.subplots()
## Plot the pie chart
ax.pie(sizes, labels=labels)
## Save the figure to a file
plt.savefig('/home/labex/project/pie_chart.png')
print("Pie chart saved to pie_chart.png")
现在,让我们运行脚本。在 WebIDE 中打开一个终端(你可以使用终端面板中的 + 图标或 Terminal > New Terminal 菜单),然后执行以下命令:
import matplotlib.pyplot as plt
## Data for the pie chart
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) ## only "explode" the 2nd slice (i.e. 'Hogs')
## Create a figure and a set of subplots
fig, ax = plt.subplots()
## Plot the pie chart with explode effect
ax.pie(sizes, explode=explode, labels=labels)
## Save the figure to a file
plt.savefig('/home/labex/project/pie_chart_explode.png')
print("Pie chart with explode effect saved to pie_chart_explode.png")
现在,从终端运行更新后的脚本:
python3 main.py
你将看到此输出:
Pie chart with explode effect saved to pie_chart_explode.png
import matplotlib.pyplot as plt
## Data for the pie chart
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) ## only "explode" the 2nd slice (i.e. 'Hogs')
## Create a figure and a set of subplots
fig, ax = plt.subplots()
## Plot the pie chart with explode and percentage
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%')
## Save the figure to a file
plt.savefig('/home/labex/project/pie_chart_percent.png')
print("Pie chart with percentages saved to pie_chart_percent.png")
再次从终端运行脚本:
python3 main.py
输出将是:
Pie chart with percentages saved to pie_chart_percent.png
import matplotlib.pyplot as plt
## Data for the pie chart
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) ## only "explode" the 2nd slice (i.e. 'Hogs')
## Create a figure and a set of subplots
fig, ax = plt.subplots()
## Plot the final pie chart with all features
ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
## Equal aspect ratio ensures that pie is drawn as a circle.
ax.axis('equal')
## Save the figure to a file
plt.savefig('/home/labex/project/pie_chart_final.png')
print("Final pie chart saved to pie_chart_final.png")