matplotlib 的奇怪之处,它没有绘制我的图表

发布于 2024-09-14 04:59:11 字数 1349 浏览 2 评论 0原文

发生的事情是我按照这个 demo 进行操作,我修改了它以满足我的需要它工作正常,将其更改为使用函数绘制两个图形,但现在使用 plt.show()plt.savefig() 根本不起作用,

这是我的代码

import csv
import numpy as np

import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

# I converted excel to a csv file
data = [x for x in csv.reader(open('ASS1_Q1.csv'))]

question1 = {}
question1['males'] = []
question1['females'] = []
for x in data:
    if x[0].lower() == "male":
        question1["males"].append(float(x[1]))
    elif x[0].lower() == "female":
        question1['females'].append(float(x[1]))
    else:
        print "Not a valid dataline", x

def plot_graph(data, filename):

    fig = plt.figure()
    ax = fig.add_subplot(111)

    n, bins, patches = ax.hist(np.array(data), bins=13, align='mid', facecolor='#888888')

    ax.set_xlabel('Speed in kph')
    ax.set_ylabel('Amount of Females')

    ax.set_xlim(min(data, max(data)))

    # plt.savefig(filename)
    plt.show()           

plot_graph(question1['males'], "ASS1Q1-males.eps")
#plot_graph(question1['females'], "ASSQ2-females.eps")
print summary(question1['males'])
print summary(question1['females'])

有人可以解释为什么会发生这种情况吗?我做错了什么?

What happened is I followed this demo, I modified it to suit my needs had it working, changed it to use a function to draw two graphs but now it doesn't work at all using plt.show() or plt.savefig()

here's my code

import csv
import numpy as np

import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

# I converted excel to a csv file
data = [x for x in csv.reader(open('ASS1_Q1.csv'))]

question1 = {}
question1['males'] = []
question1['females'] = []
for x in data:
    if x[0].lower() == "male":
        question1["males"].append(float(x[1]))
    elif x[0].lower() == "female":
        question1['females'].append(float(x[1]))
    else:
        print "Not a valid dataline", x

def plot_graph(data, filename):

    fig = plt.figure()
    ax = fig.add_subplot(111)

    n, bins, patches = ax.hist(np.array(data), bins=13, align='mid', facecolor='#888888')

    ax.set_xlabel('Speed in kph')
    ax.set_ylabel('Amount of Females')

    ax.set_xlim(min(data, max(data)))

    # plt.savefig(filename)
    plt.show()           

plot_graph(question1['males'], "ASS1Q1-males.eps")
#plot_graph(question1['females'], "ASSQ2-females.eps")
print summary(question1['males'])
print summary(question1['females'])

Can someone explain why this is happening? what am I doing wrong?

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

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

发布评论

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

评论(1

对你的占有欲 2024-09-21 04:59:11

尝试删除

import matplotlib
matplotlib.use('Agg')

该命令

python -c 'import matplotlib; matplotlib.use("")'

将显示可以发送到 matplotlib.use 的有效字符串参数。
在我的机器上,“Agg”被列为有效,但设置此值时我没有得到任何输出。如果您很好奇,可以继续尝试各种选项,直到找到一种有效的方法。

当您找到您喜欢的那个时,您可能还会发现

backend      : GtkAgg

在 ~/.matplotlib/matplotlibrc 中设置类似的内容比使用 matplotlib.use(...) 更方便。

Try removing

import matplotlib
matplotlib.use('Agg')

The command

python -c 'import matplotlib; matplotlib.use("")'

will show you the valid string arguments that can be sent to matplotlib.use.
On my machine, 'Agg' is listed as valid, though I get no output when this is set. If you are curious, you could just keep trying various options until you find one that works.

When you find the one that your prefer, you may also find it more convenient to set something like

backend      : GtkAgg

in your ~/.matplotlib/matplotlibrc instead of using matplotlib.use(...).

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