Matplotlib 绘图未显示在 Mac OSX 中?

发布于 2024-08-26 15:14:32 字数 334 浏览 6 评论 0原文

我运行的是 Mac OSX 10.5.8。我使用 macports 安装了 matplotlib。我从 matplotlib 库中得到了一些示例,如下所示,无需修改:

http://matplotlib。 sourceforge.net/examples/api/unicode_minus.html

我运行它,没有错误,但图片不显示。在 Linux Ubuntu 中我明白了。

你知道这里可能出了什么问题吗?

I am running Mac OSX 10.5.8. I installed matplotlib using macports. I get some examples from the matplotlib gallery like this one, without modification:

http://matplotlib.sourceforge.net/examples/api/unicode_minus.html

I run it, get no error, but the picture does not show up. In Linux Ubuntu I get it.

Do you know what could be wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

一场信仰旅途 2024-09-02 15:14:32

我遇到了同样的问题,即使我可以看到新的应用程序窗口是如何创建并立即消失的。

是否有

# Assumes you have imported "matplotlib.pyplot" as "plt"
plt.show()

简单的解决方案 - 只需检查情节后

I had the same problem, even I could see how a new application window was created and immediately disappeared.

Simple solution - just check if you have

# Assumes you have imported "matplotlib.pyplot" as "plt"
plt.show()

after the plot

我要还你自由 2024-09-02 15:14:32

我也可以在我这边验证这一点。为了解决这个问题,我做了以下操作。

sudo port install py25-matplotlib +cairo+gtk2
sudo port install py26-matplotlib +cairo+gtk2

另外,我们需要将默认后端更改为基于 GUI 的后端。

编辑文件 ~/.matplotlib/matplotlibrc,并添加:

backend: GTKCairo

另外,您可以尝试以下操作,这可能允许您不需要 GTK 或 Cairo 后端。
编辑 ~/.matplotlib/matplotlibrc 并添加:

backend: MacOSX

安装了这些变体的端口后,这也可以工作,但不需要 X11。


顺便说一句,我看到的错误如下:

/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/__init__.py:41: UserWarning: 
Your currently selected backend, 'Agg' does not support show().
Please select a GUI backend in your matplotlibrc file ('/Users/wlynch/.matplotlib/matplotlibrc') or with matplotlib.use()
(backend, matplotlib.matplotlib_fname()))

I can verify this on my end as well. To fix, here's what I did

sudo port install py25-matplotlib +cairo+gtk2
sudo port install py26-matplotlib +cairo+gtk2

Also, we need to change the default backend to a GUI based one.

Edit the file ~/.matplotlib/matplotlibrc, and add:

backend: GTKCairo

Also, you can try the following, which may allow you to not need the GTK or Cairo backends.
Edit ~/.matplotlib/matplotlibrc and add:

backend: MacOSX

With the port with those variants installed, this works as well, but it doesn't require X11.


By the way, the error that I saw was the following:

/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/__init__.py:41: UserWarning: 
Your currently selected backend, 'Agg' does not support show().
Please select a GUI backend in your matplotlibrc file ('/Users/wlynch/.matplotlib/matplotlibrc') or with matplotlib.use()
(backend, matplotlib.matplotlib_fname()))
寻找我们的幸福 2024-09-02 15:14:32

这对我有用。我刚刚更改了 Matplotlib 的导入

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

This is what worked for me. I just changed the import of Matplotlib

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
装迷糊 2024-09-02 15:14:32

当您尝试

plt.savefig('myfilename.png')

而不是

plt.show()

在当前路径中保存名为 myfilename.png 的正确图像时?

When you try

plt.savefig('myfilename.png')

instead of

plt.show()

does that save the correct image named myfilename.png in the current path?

有木有妳兜一样 2024-09-02 15:14:32

在绘图后只需添加 -

plt.show()

其工作原理是与交互模式和非交互模式有关。如果后端以非交互模式打开,则代码块末尾需要 plt.show()。您可以通过调用 plt.isinteractive() 检查状态,并使用 plt.ion()plt.ioff() 切换状态

After the plot simply add -

plt.show()

The reason this works is to do with interactive vs non-interactive mode. If the backend is opened in non-interactive mode, plt.show() is required at the end of the code chunk. You can check the status by calling plt.isinteractive() and toggle the status using plt.ion() and plt.ioff()

薄荷→糖丶微凉 2024-09-02 15:14:32

我想为我分享这个可行的解决方案,

import matplotlib
import platform
if platform.system() == 'Darwin':
    matplotlib.use('MacOSX')

I wanna share this workable solution for me,

import matplotlib
import platform
if platform.system() == 'Darwin':
    matplotlib.use('MacOSX')
毁梦 2024-09-02 15:14:32

只是添加注释,

我的系统上不存在 matplotlibrc 文件,我必须从 matplotlib 网站下载一个副本。未来的用户可能也必须这样做。

just to add a note,

The matplotlibrc file was not present on my system and I had to to download a copy from the matplotlib website. Future users may have to do the same.

怀里藏娇 2024-09-02 15:14:32

这对我有用:

brew install pkg-config
brew link pkg-config
brew install pygtk
brew install freetype
brew install libpng

sudo ln -s /usr/local/Cellar/freetype/*/lib/pkgconfig/freetype2.pc /usr/local/lib/pkgconfig/freetype2.pc

git clone [email protected]:matplotlib/matplotlib.git
cd matplotlib
python setup.py build
python setup.py install

参考文献:

http://blog .caoyuan.me/2012/08/matplotlib-error-mac-os-x/
http://matplotlib.org/faq/installing_faq.html#install-from-git
http://www.tapir.caltech.edu/~dtsang/python.html

This is what worked for me:

brew install pkg-config
brew link pkg-config
brew install pygtk
brew install freetype
brew install libpng

sudo ln -s /usr/local/Cellar/freetype/*/lib/pkgconfig/freetype2.pc /usr/local/lib/pkgconfig/freetype2.pc

git clone [email protected]:matplotlib/matplotlib.git
cd matplotlib
python setup.py build
python setup.py install

References:

http://blog.caoyuan.me/2012/08/matplotlib-error-mac-os-x/
http://matplotlib.org/faq/installing_faq.html#install-from-git
http://www.tapir.caltech.edu/~dtsang/python.html

孤独陪着我 2024-09-02 15:14:32

我只有 python 2.5,我不想在我的 mac 上安装 python 2.6。因此,我使用以下链接中提到的不同过程来解决此问题:

http: //www.gtkforums.com/viewtopic.php?f=3&t=54928

实际需要的是以下步骤:

1)搜索目录“pygtk-”在哪里2.0.pc”并找到它。例如我的位于以下目录:

/opt/local/lib/pkgconfig

2) 将路径信息添加到环境变量中。例如:

PKG_CONFIG_PATH=/opt/local/lib/pkgconfig
export PKG_CONFIG_PATH

3)从matplotlib网站下载配置信息文件“matplotlibrc”
http://matplotlib.sourceforge.net/_static/matplotlibrc

4) 将后端更改为 MacOSX文件并保存

5) 将文件复制到目录 .matplotlib
您可以通过以下命令找到python中的目录:

import matplotlib
matplotlib.get_configdir()

I only had python 2.5 and I did not want to install python 2.6 on my mac. So I used different procedure mentioned in the following link to solve this problem:

http://www.gtkforums.com/viewtopic.php?f=3&t=54928

What that one actually needs is the following steps:

1) Searching where is the directory "pygtk-2.0.pc" and locate it. For example mine was located in the following directory:

/opt/local/lib/pkgconfig

2) Adding the path information to envirement variable. For example:

PKG_CONFIG_PATH=/opt/local/lib/pkgconfig
export PKG_CONFIG_PATH

3) Download the configuration information file "matplotlibrc" from matplotlib website
http://matplotlib.sourceforge.net/_static/matplotlibrc

4) Change backend to MacOSX in the file and save it

5) Copy the file to directory .matplotlib
You can locate the directory in python by the following command:

import matplotlib
matplotlib.get_configdir()
晚雾 2024-09-02 15:14:32

Mac 自带了自己的 Python。更多详细信息可以在 Matplotlib 常见问题解答中找到,但它是不是最好的解释。

我建议只干净安装一些 Python 3.7 左右以及 Anaconda,然后将它们作为 PyCharm 的解释器引入。一切都会正常工作,您不需要添加临时解决方案,例如 backend: MacOSX 等。

Mac comes with its own Python. More details can be found in the Matplotlib FAQ, but it's not the best explanation.

I would suggest just a clean install of some Python 3.7 or so along with Anaconda and then introduce them as interpreters to PyCharm. Anything will work fine and you wont need to add ad-hoc solutions like backend: MacOSX or so.

请止步禁区 2024-09-02 15:14:32
sudo port install py37-matplotlib +cairo+gtk3
~/.matplotlib/matplotlibrc used 
backend: MacOSX

似乎可以在 unicode_minus.py 示例上使用 python 3.7 在 MacOS Mojave 10.14.4 上工作a> 如上所述。

sudo port install py37-matplotlib +cairo+gtk3
~/.matplotlib/matplotlibrc used 
backend: MacOSX

Seemed to work on MacOS Mojave 10.14.4 with python 3.7 on the unicode_minus.py example above.

む无字情书 2024-09-02 15:14:32

如果有人使用spyder,请执行以下操作。

1.) 从 Anaconda Launcher 启动 Spyder 2.3.5.2
2.) 转到首选项 -> IPython 控制台 ->图形->后端:将其更改为“自动”
3.) 选择“应用”并关闭首选项
3.) 重启IPython内核
4.) 创建简单的图形,例如

Do the following if anyone is using spyder.

1.) Start Spyder 2.3.5.2 from Anaconda Launcher
2.) Go to preferences -> IPython console -> Graphics -> Backend: changed it to "Automatic"
3.) Select "Apply" and close preferences
3.) Restart IPython kernel
4.) Create simple graphic like

○愚か者の日 2024-09-02 15:14:32

作为一种临时解决方案,可以将图形保存为 .png/.jpg/.pdf 并暂时使用该文件。

## assuming price is out DataFrame that contains columns that we want to plot 
pdf_plot=price.plot().get_figure()  
pdf_plot.savefig('Stocks.pdf')

As a temporary work around one can save the figure to a .png/.jpg/.pdf and make use of that file for the moment.

## assuming price is out DataFrame that contains columns that we want to plot 
pdf_plot=price.plot().get_figure()  
pdf_plot.savefig('Stocks.pdf')
苯莒 2024-09-02 15:14:32

康达
(Ana)conda 中提供的默认 python 不是框架构建。但是,无论是在主环境还是在 conda envs 中,都可以轻松安装框架构建:安装 python.app (conda install python.app) 并使用 pythonw 而不是 python。

conda
The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文