不留余地Pycharm不能展现matplotlib绘图难点,不展现图的消除办法

目前始发接触matplotlib,

减轻Pycharm不可能呈现matplotlib绘图难点,pycharmmatplotlib

近些年开端接触matplotlib,

一.首先安装matplotlib库和其借助的部分其它库,比方:numpy,scipy和pandas等

二.上马开始展览轻松的编码工作,并在PyCharm中运作,出现如下错误:

Traceback (most recent call last):
  File "J:/MyGit/pythonTest/GetTegeather/test06.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "J:\software\PyCharm 2017.3.1\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 17, in <module>
    verbose = matplotlib.verbose
AttributeError: module 'matplotlib' has no attribute 'verbose'

解决步骤如下:

前提:

壹.导入准确版本的matplotlib库

二.代码最后调用matplotlibku中pyplot.show()方法

三,无任何低端错误

化解步骤:

一.在导入pyplot之前.须求输入如下步骤:

  import matplotlib as mpl

  mpl.use(‘Agg’)

  import matplotlib.pyplot as
plt

  ….

2.那时候运作python程序,尽管不报错,不过并不曾绘图分界面弹出

  在命令号几面输入如下命令:

  import matplotlib

  print(matplotlib.get_backend())

  将赢得的字符串结果替换mlp.use(‘Agg’)中Agg’就可以

  例如:我的print(matplotlib.get_backend())结果为TkAgg,替换后为:

  mpl.use(‘TkAgg’)

三.展现正常

四.至于原因,个人才干有限,无法解释通透到底,

 

不留余地Pycharm不能展现matplotlib绘图难点,不展现图的消除办法。有大神直接改变matplotlibrc文件中backend的值(backend选项改成你系统的图片蒙受),风趣味的自发性检索

方今开班接触matplotlib,
一.先是安装matplotlib库和其借助的片段其他库,比方:numpy,scipy和pan…

matplotlib入门(条形图, 直方图, 盒须图, 饼图),matplotlib条形

作图首先要实行多少的输入,matplotlib包只提供作图相关职能,本人并从未数据读入、输出函数,针对各样考试或总计文本数据输入能够运用numpy提供的数目输入函数。

# -*- coding: gbk -*-
"""
Created on Sun Jan 11 11:17:42 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

#生成数据
dataOut = np.arange(24).reshape(4, 6)
print(dataOut)

#保存数据
np.savetxt('data.txt', dataOut, fmt = '%.1f')

#读取数据
data = np.loadtxt('data.txt')
print(data)

plot 和 bar 函数

# -*- coding: gbk -*-
"""
Created on Sun Jan 11 11:33:14 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

data = np.random.randint(1, 11, 5)
x = np.arange(len(data))

plt.plot(x, data, color = 'r')
plt.bar(x, data, alpha = .5, color = 'g')

plt.show()

结果图片

# -*- coding: gbk -*- “”” Created on Sun Jan 11 11:33:14 2015
@author: zhang “”” import numpy as np import matplotlib.pyplot as plt
import matplotlib as mpl mpl.rcParams[‘font.family’] = ‘sans-serif’
mpl.rcParams[‘font.sans-serif’] = [u’SimHei’] data =
np.random.randint(1, 11, 5) x = np.arange(len(data)) #plt.plot(x, data,
color = ‘r’) #plt.bar(x, data, alpha = .5, color = ‘g’) plt.pie(data,
explode = [澳门葡京备用网址 ,0,0,.2, 0, 0]) plt.show

# -*- coding: gbk -*- “”” Created on Sun Jan 11 11:51:41 2015
@author: zhang “”” import numpy as np import matplotlib.pyplot as plt
import matplotlib as mpl mpl.rcParams[‘font.family’] = ‘sans-serif’
mpl.rcParams[‘font.sans-serif’] = [u’SimHei’] data =
np.random.randint(1, 5, (5, 2)) x = np.arange(len(data)) plt.plot(x,
data[:, 0], ‘–‘, color = ‘m’) plt.plot(x, data[:, 1], ‘-.’, color =
‘c’) plt.show() # -*- coding: gbk -*- “”” Created on Sun Jan 11
12:03:57 2015 @author: zhang “”” import numpy as np import
matplotlib.pyplot as plt import matplotlib as mpl
mpl.rcParams[‘font.family’] = ‘sans-serif’
mpl.rcParams[‘font.sans-serif’] = [u’SimHei’]
mpl.rcParams[‘axes.unicode_minus’] = False data =
np.random.randint(1, 5, [3, 4]) index = np.arange(data.shape[1])
color_index = [‘r’, ‘g’, ‘b’] fig, (ax1, ax2, ax3) = plt.subplots(3,
1, figsize = (5, 12)) for i in range(data.shape[0]): ax1.bar(index +
i*.25 + .1, data[i], width = .25, color = color_index[i],\ alpha
= .5) for i in range(data.shape[0]): ax2.bar(index + .25, data[i],
width = .5, color = color_index[i],\ bottom = np.sum(data[:i],
axis = 0), alpha = .7) ax3.barh(index, data[0], color = ‘r’, alpha =
.5) ax3.barh(index, -data[1], color = ‘b’, alpha = .5) plt.show()
plt.savefig(‘complex_bar_chart’)

# -*- coding: gbk -*- “”” Created on Sun Jan 11 12:29:34 2015
@author: zhang “”” import numpy as np import matplotlib.pyplot as plt
import matplotlib as mpl mpl.rcParams[‘font.family’] = ‘sans-serif’
mpl.rcParams[‘font.sans-serif’] = [u’SimHei’] data =
np.random.randn(100) fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (8,
4)) ax1.hist(data) ax2.boxplot(data) plt.savefig(‘hist_boxplot’)
plt.show()
澳门葡京备用网址 1

正文讲到的装有matplotlib命令都有卓殊丰硕的定制参数,笔者会在前面作品中讲到,你也足以查看扶助文书档案学习。

直方图, 盒须图,
饼图),matplotlib条形
作图首先要开始展览多少的输入,matplotlib包只提供作图相关职能,本人并未…

问题

近些年,作者重新安装了ubuntu,使用virtualenv安装了matplotlib。然后,难题来了。当笔者运转下列代码时,未有图框跳出来。
<code>
import matplotlib.pyplot as plt
plt.show()
plt.bar(left = 0,height = 1)
</code>

一.先是安装matplotlib库和其借助的片段其余库,举例:numpy,scipy和pandas等

原因

自家使用%pylab查看matplotlib后端,开掘竟然是agg。兄弟姐妹们,agg是不会画画的!
<code>
In [4]:%pylab
Using matplotlib backend: agg
Populating the interactive namespace from numpy and matplotlib
</code>

二.早先进行简短的编码工作,并在PyCharm中运作,出现如下错误:

解决

Traceback (most recent call last):
  File "J:/MyGit/pythonTest/GetTegeather/test06.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "J:\software\PyCharm 2017.3.1\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 17, in <module>
    verbose = matplotlib.verbose
AttributeError: module 'matplotlib' has no attribute 'verbose'

1. 安装Tkinter 和 matplotlib

本人的难题在于小编原本的python居然未有Tkinter!!
<code>
sudo apt-get install tk-dev
pip uninstall -y matplotlib
pip –no-cache-dir install -U matplotlib #那是最注重的
</code>

消除步骤如下:

2. 设置agg

其实通过地方的步调,已经得以画图了
补充2种设置agg方法

  • 临时的
    <code>
    import matplotlib
    matplotlib.use(‘TkAgg’)
    </code>
    本条命令必须在率先次使用%pylab 也许import matplotlib.pyplot as
    plt在此之前运用
    常见的agg有:Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG

  • 永久的
    修改matplotlibrc文件
    matplotlibrc文件的职分在:
    <code>[~/.virtualenvs/myenv]/lib/python2.7/site-packages/matplotlib/mpl-data/</code>
    修改地点:
    <code>
    backend : youragg
    </code>

从那之后,matplotlib能够健康干活了。

前提:

一.导入正确版本的matplotlib库

二.代码最后调用matplotlibku中pyplot.show()方法

三,无别的低等错误

消除步骤:

一.在导入pyplot在此之前.要求输入如下步骤:

  import matplotlib as mpl

  mpl.use(‘Agg’)

  import matplotlib.pyplot as
plt

  ….

二.那时运作python程序,尽管不报错,可是并不曾绘图分界面弹出

  在命令号几面输入如下命令:

  import matplotlib

  print(matplotlib.get_backend())

  将得到的字符串结果替换mlp.use(‘Agg’)中Agg’就可以

  例如:我的print(matplotlib.get_backend())结果为TkAgg,替换后为:

  mpl.use(‘TkAgg’)

3.体现寻常

四.至于原因,个人技艺有限,不可能解释深透,

 

有大神直接改动matplotlibrc文件中backend的值(backend选项改成你系统的图片情况),风乐趣的机关检索

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website