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")