сюжет не отображается, когда я вызываю plt.show () в matplotlib - PullRequest
2 голосов
/ 07 августа 2020

Я сейчас изучаю python, и я видел этот код в книге, но он не работает для меня. это про динамику c визуализацию прокатки ie:

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()

после запуска скрипта ничего не происходит. Обратите внимание, что matplotlib и seaborn в моей системе работают правильно. Я заранее использовал их для c визуализации.

enter image description here

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()

введите описание изображения здесь

Я не знаю, почему график не показан в первом коде.

Ответы [ 2 ]

1 голос
/ 07 августа 2020

только что импортированная анимация из matplotlib, и ваш код работал нормально

from matplotlib import animation

введите описание изображения здесь

0 голосов
/ 07 августа 2020

Похоже, проблема в версии pycharm pro. он не работает в профессиональной версии, но работает в версии сообщества. спасибо Адхуну Тхалеккаре за то, что заметил это. Я использовал cmd для запуска кода, и он у меня сработал правильно.

...