[英]plot is not shown when I call plt.show() in matplotlib
I'm currently learning python and i saw this code in a book but it's not working for me.我目前正在学习 python,我在一本书中看到了这段代码,但它对我不起作用。 it's about dynamic visualization of rolling a die:这是关于滚动骰子的动态可视化:
from matplotlib import animation
import matplotlib.pyplot as plt
import random
import seaborn as sns
def update(frame_number, rolls, faces, frequencies):
for i in range(rolls):
frequencies[random.randrange(1, 7) - 1] += 1
plt.cla()
axes = sns.barplot(faces, frequencies, palette='bright')
axes.set_title(f'Die Frequencies for {sum(frequencies):,} Rolls')
axes.set(xlabel='Die Value', ylabel='Frequency')
axes.set_ylim(top=max(frequencies) * 1.10)
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f'{frequency:,}\n{frequency / sum(frequencies):.3%}'
axes.text(text_x, text_y, text, ha='center', va='bottom')
number_of_frames = 10000
rolls_per_frame = 600
sns.set_style('whitegrid')
figure = plt.figure('Rolling a Six-Sided Die')
values = list(range(1, 7))
frequencies = [0] * 6
die_animation = animation.FuncAnimation(figure, update, repeat=False, frames=number_of_frames, interval=33,
fargs=(rolls_per_frame, values, frequencies))
plt.show()
after running the script nothing happens.运行脚本后没有任何反应。 Note that matplotlib and seaborn are working correctly in my system.请注意,matplotlib 和 seaborn 在我的系统中工作正常。 I used them for static visualization beforehand.我事先将它们用于 static 可视化。
There is no problem with this code:这段代码没有问题:
import matplotlib.pyplot as plt
import numpy as np
import random
import seaborn as sns
rolls = [random.randrange(1, 7) for i in range(600)]
values, frequencies = np.unique(rolls, return_counts=True)
title = f'Rolling a six-sided die {len(rolls):,} Times'
sns.set_style('whitegrid')
axes = sns.barplot(x=values, y=frequencies, palette='bright')
axes.set_title(title)
axes.set(xlabel='Die value', ylabel='Frequency')
axes.set_ylim(top=max(frequencies) * 1.10)
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f'{frequency:,}\n{frequency / len(rolls):.3%}'
axes.text(text_x, text_y, text, fontsize=11, ha='center', va='bottom')
plt.show()
I don't know why plot is not shown in the first code.我不知道为什么第一个代码中没有显示 plot 。
It seems the problem is with pycharm pro version.似乎问题出在 pycharm 专业版上。 it's not working in pro version but it works in community version.它不适用于专业版,但可以在社区版中使用。 thanks to Adhun Thalekkara for noticing that.感谢 Adhun Thalekkara 注意到这一点。 I used the cmd to run the code and it worked correctly for me.我使用 cmd 运行代码,它对我来说工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.