简体   繁体   English

matplotlib:无法绘制线或条; 有效轴

[英]matplotlib: fail to plot line or bar; valid axes

I'm attempting generate matplotlib images through a loop. 我正在尝试通过循环生成matplotlib图像。 I have two iterations of loops that generate images. 我有两次循环生成图像。 The first loop works, the second doesn't. 第一个循环有效,第二个无效。 The axes are valid, I can see that when I print the numpy arrays. 轴是有效的,我可以在打印numpy数组时看到。

plt_mean = float(week_occurrences) / len(x_axis)
y_np = np.array(y_axis)
std_d = np.std(y_np)
plt.plot(x_axis, y_np, color='#758AA8')
plt.axis([y_axis[0], y_axis[6], 0, int(y_max * 1.2)])
plt.axhline(plt_mean, color='black')
plt.ylabel("Events")
plt.xlabel("Day")
plt.title(event)
plt.savefig("tmp/{} {}.jpg".format(event, y_axis[0]), bbox_inches='tight')
plt.clf()

print(event)
print(y_max)
print(plt_mean)
print(x_axis)
raw_input(y_np)

output: 输出:

A user account was changed.
384
111.571428571
[5, 22, 4, 384, 363, 3, 0]
[166 167 168 169 170 171 172]

在此处输入图片说明

What am I missing? 我想念什么? Why won't it plot the associated lines? 为什么不绘制相关线?

I believe the line is plotted, but I think your axis limits are wrong. 我相信该线已绘制,但我认为您的轴限制是错误的。 I'm not entirely sure what you're trying to do, because it looks like you've inverted your x and y. 我不确定您要做什么,因为您好像已经颠倒了x和y。

here is the result after the line: 这是行后的结果:

plt.plot(x_axis, y_np, color='#758AA8')

在此处输入图片说明

However, after the line 但是,行后

plt.axis([y_axis[0], y_axis[6], 0, int(y_max * 1.2)])

the axes limit do not make any sense anymore and you're seeing a region where there are no data. 轴限制不再有意义,您会看到一个没有数据的区域。 plt.axis() takes its argument in the order [xmin, xmax, ymin, ymax] plt.axis()的参数顺序为[xmin, xmax, ymin, ymax]

Looks like you didn't define y_max correctly. 看来您没有正确定义y_max。 This works for me: 这对我有用:

import numpy as np
import matplotlib.pylab as plt

x_axis = [5, 22, 4, 384, 363, 3, 0]
y_axis = [166, 167, 168, 169, 170, 171, 172]
y_max = np.max(y_axis)
event = np.str('A user account was changed.')
week_occurrences = 780.999999997
plt_mean = float(week_occurrences) / len(x_axis)
y_np = np.array(y_axis)
std_d = np.std(y_np)
plt.plot(x_axis, y_np, color='#758AA8')
plt.axis([y_axis[0], y_axis[6], 0, int(y_max * 1.2)])
plt.axhline(plt_mean, color='black')
plt.ylabel("Events")
plt.xlabel("Day")
plt.title(event)
# plt.savefig("tmp/{} {}.jpg".format(event, y_axis[0]), bbox_inches='tight')
# plt.clf()

print(event)
print(y_max)
print(plt_mean)
print(x_axis)

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

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