本文介绍了无法使用matplotlib和 pandas 进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OSX (Mojave 10.14.3) 并且在使用 matplotlib (3.0.3) 绘制 pandas (0.24.2) 数据帧时遇到了一个奇怪的问题.我正在使用python 3.7.3 .

I am using OSX (Mojave 10.14.3) and am having a strange issue plotting a pandas (0.24.2) dataframe using matplotlib (3.0.3). I am using python 3.7.3.

因此,代码如下:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
                  'name':['john','mary','peter','jeff','bill','lisa','jose'],
                  'age':[23,78,22,19,45,33,20],
                  'gender':['M','F','M','M','M','F','M'],
                  'state':['california','dc','california','dc','california','texas','texas'],
                  'num_children':[2,0,0,3,2,1,4],
                  'num_pets':[5,1,0,5,2,2,3]
                  })

df.plot(kind='scatter',x='num_children',y='num_pets',color='red')
plt.show()

所有这些操作就是显示一个没有任何内容的空窗口.我原本希望得到7点的散点图.该示例取自网络教程.

All this does is show an empty window with nothing in it. I was expecting a scatterplot with 7 points. The example is taken from the web tutorial as is.

编辑

plt.savefig('myfilename.png')

Savefig 有效.

Savefig works.

推荐答案

我不确定这是否对任何人都有用,但我基本上必须安装python作为使其工作的框架.我使用的是 Anaconda,所以类似于:

I am not sure if this will help anyone but I basically had to install python as a framework to make it work. I was using Anaconda, so something like:

conda install python.app
pythonw script.py  # note using pythonw

然后,

I通过使用 macosx 后端能够使绘图正确渲染:

I, then, was able to get the plot to render correctly by using the macosx backend:

import matplotlib as mpl
mpl.use('MacOSX')

这篇关于无法使用matplotlib和 pandas 进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:23