我可以在jupyter中使用for循环打印饼图吗?
我需要用于循环来在功能中(最好是水平的)打印多个饼图。我的假设是,如果我使用for循环打印饼图,将产生所有图表,但结果将垂直显示。但是,只显示了最后一个数字。
import matplotlib.pyplot as plt
for i in range(3):
labels = ['part_1','part_2','part_3']
pie_portions = [5,6,7]
plt.pie(pie_portions,labels=labels,autopct = '%1.1f%%')
plt.title(f'figure_no : {i+1}')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在遇到jupyter笔记本中内置的depl范式,用于最后引用的对象。
print
(和/或在笔记本的情况下显示
)在“ read-evaluate-print loop'(depp)默认情况下,通常仅适用于jupyter在输出中。这与为什么您具有定义变量的原因有关,您可以将其调用为单元的最后一行,并且它的值将显示在而无需print(my_variable)
。关于您的假设。要使它们都显示垂直,请尝试:
此处的所有代码块都是开发出来的,并且将在您的浏览器中正常工作,而无需通过 mybinder-sessions通过此处启动 环境由此列表 pip安装的软件包。这些特定示例中未使用其中的许多。
基于iPywidgets Hbox的水平解决方案
此初始示例的大部分是基于调整我的答案在这里,单独 widget tabs 然后可以选择以查看以查看反过来。
这将使用Hbox并排显示三个直方图。它还使用以下方法中更多使用的子图,并且可以在不使用小部件的情况下完成工作。 (我想将小部件包含在一个选项中,它显示了我已经拥有的框架的“选项卡”显示代码如何轻松地适应Hbox代码,我可以想象,如果您使用的是使用仪表板小部件也涉及。)
但是,OP想要派。这是一个更接近我对PIE图的解决方案。但是,我发现它是故障的,需要解决方法:
不确定为什么从该代码中出现明显的小故障,添加第四个虚拟派情节允许所有三个馅饼地块至少可以显示。但是,这并不理想,我发现如果您将子图(请参见下文)与小部件输出相结合,那么它可以并排显示三个派图,而无需进行解决方法。通过将子图与小窗口组合结合到无实际解决的清洁版本:
基于matplotlib的子图的水平解决方案
。这是从中改编的三个并排进行的:
一个概括为像您这样的循环的概括:
请注意,这些解决方案是用剧情/情节对象的活动“内存”形式进行的。您也可以将图作为图像文件保存为图像文件,并使用HTML与
< img Align一起在笔记本单元中并排显示结果图像。 href =“ https://stackoverflow.com/a/44969840/8508004”>在这里and 在这里基于在这里。 (在笔记本中并排显示图像的方法被回收在这里来自图像文件集合的幻灯片。)
You are encountering the REPL paradigm built into Jupyter notebooks for the last referenced object. The
print
(and/ordisplay
in the case of notebooks) in 'Read-evaluate-print loop'(REPL) by default usually only applies in Jupyter to the last thing in the output. This is related to why if you have a defined variable, you can just invoke it as the last line of the cell and its value will be shown without you needingprint(my_variable)
.In regards to your assumption. To have them all show vertical, try:
All the code blocks here were developed and will work right in your browser with no installations necessary via mybinder-served sessions launched via here where the environment is determined by this list of pip installed packages. Many of those listed are not used in these particular examples.
Horizontal solution based on ipywidgets HBox
Much of this initial example is based on adapting my answer here, where the OP wanted the plots on separate widget tabs that could then be selected to view in turn.
That will show three histograms side by side using HBox. It also uses subplots which are used more in the approach below and could do the job without use of widgets. (I wanted to include widgets as an option it shows how the 'tab' display code I already had the framework for can easily be adapted to HBox code, and I can imagine how having options could come in handy depending if you are making dashboards with widgets involved as well.)
However, OP wanted pie plots. This is a solution closer to what I did with the pie plot here; however, I found it to be glitchy and require a workaround:
Not quite sure why that apparent glitch comes up from that code where adding a fourth dummy pie plot allows all three pie plots to at least show. However, that is not ideal and I found if you combine subplots (see below) with the widgets output, then it works to show the three pie plots side by side without needing that workaround. Cleaner version without the workaround by combining subplots with the widgets output:
Horizontal solution based on Matplotlib's subplots
The documentation has an example using subplots to display multiple pie plots. This was adapted from that to do three side-by-side:
That one generalized to a for loop like yours:
Note that these solutions were done with active 'in memory' forms of the plots/plot objects. You could also save the plots as image files and display the resulting images side-by-side in a notebook cell using HTML combined with
<img align ..>
tags, based on based on here,here, and here, or HTML combined with tables, based on here. (That approach to displaying images side-by-side in a notebook gets recycled here to automate making Jupyter RISE slideshows from a collection of image files.)