简体   繁体   English

Python:导入了matplotlib,但无法使用imshow

[英]Python: Imported matplotlib but cannot use imshow

I'm working in Ubuntu as Virtual-Box guest on top of Windows machine (as host), and I am attempting to run my python script from the terminal. 我正在Ubuntu上以Windows机器(作为主机)之上的Virtual-Box guest虚拟机工作,并且正在尝试从终端运行python脚本。 I had difficulty installing matplotlib using pip install. 我在使用pip install安装matplotlib时遇到困难。 I managed to install it using 我设法使用安装

sudo apt-get install python-matplotlib 须藤apt-get install python-matplotlib

However, I am unable to bring up an image I have created in my code: 但是,我无法显示在代码中创建的图像:

import numpy as np 
import matplotlib.pyplot as plt
import random as random
from random import randrange


image = plt.imshow(mymatrix)
plt.show() 

If I import matplotlib as: 如果我将matplotlib导入为:

import matplotlib as plt

I am recieving the following error on attempting to run the script: 我在尝试运行脚本时收到以下错误:

AttributeError: 'module' object has no attribute 'imshow' AttributeError:'模块'对象没有属性'imshow'

If I import matplotlib as: 如果我将matplotlib导入为:

import matplotlib.pyplot as plt

I recieve the following error: 我收到以下错误:

raise ImportError, str(msg) + ', please install the python-tk package' ImportError: No module named _tkinter, please install the python-tk package 引发ImportError,str(msg)+',请安装python-tk软件包'ImportError:没有名为_tkinter的模块,请安装python-tk软件包

On attempting to install python-tk using 'pip install python-tk' this is what I'm getting: 在尝试使用“ pip install python-tk”安装python-tk时,这就是我得到的:

~/ising $ pip install python-tk Collecting python-tk Could not find a version that satisfies the requirement python-tk (from versions: ) No matching distribution found for python-tk 〜/ ising $ pip install python-tk收集python-tk找不到满足要求的版本python-tk(来自版本:)找不到匹配的发行版python-tk

I'm unsure if I have in fact incorrectly installed matplotlib from the beginning. 我不确定实际上是否从一开始就错误地安装了matplotlib。 I am aware pyplot is not automatically imported with matplotlib, could the same be true for installing it from the console? 我知道pyplot不会自动与matplotlib一起导入,从控制台安装也一样吗? Seems like I have tried everything at this stage. 似乎我在此阶段已经尝试了一切。

The problem appears to be that you do not have a graphical backend installed. 问题似乎是您没有安装图形后端 The error you are getting about Python Tk is happening because normally Tk comes with any Python distribution, so you should have at least that. 您正在遇到的有关Python Tk的错误正在发生,因为通常Tk随任何Python发行版一起提供,因此您至少应该有这个。 You can install any Python bindings for Tk, Pyqt4, Pyqt5, Wx, GTK, (possibly others) to get a working interactive graphical backend. 您可以为Tk,Pyqt4,Pyqt5,Wx,GTK(可能还有其他)安装任何Python绑定,以获得可工作的交互式图形后端。 Check the package repository for the actual package names to install. 检查软件包存储库,以获取要安装的实际软件包名称。

Keep in mind that functions like imshow are part of the (sub)package matplotlib.pyplot , not matplotlib itself. 请记住,像imshow这样的功能是(子)包matplotlib.pyplot ,而不是matplotlib本身。 import matplotlib as plt is simply wrong if you intend to do plt.imshow(...) . 如果您打算执行plt.imshow(...)则将import matplotlib as plt是错误的。 The correct import is either from matplotlib import pyplot as plt or import matplotlib.pyplot as plt . 正确的导入是from matplotlib import pyplot as pltfrom matplotlib import pyplot as plt import matplotlib.pyplot as plt

According to the documentation here try this 根据此处的文档尝试此

 python -mpip install -U pip
 python -mpip install -U matplotlib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM