我正在制作一个程序,用于搜索特定目录中的所有声音文件并将其删除。这是我的代码:

import os, sys, time, threading
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
import shutil
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import csv, time


home1 = os.path.join(os.environ["HOMEPATH"], "Desktop")
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')

print "Welcome to SoundScanner!"
print "Select directory to scan from:\n"

mp3_list = []
mp4_list = []
avi_list = []
mov_list = []
wmv_list = []
ogg_list = []
wav_list = []

all_sound_list = []

root = Tk()
root.withdraw()
path = tkFileDialog.askdirectory(initialdir=desktop, title="Select directory to scan from: ")
path = path.encode('utf-8')


mpl.rcParams['toolbar'] = 'None'

for root, dirs, files in os.walk(path):
    for filez1 in files:
        if filez1.endswith(".mp3"):
            mp3_list.append(filez1)
            all_sound_list.append(filez1)

for root, dirs, files in os.walk(path):
    for filez2 in files:
        if filez2.endswith(".mp4"):
            mp4_list.append(filez2)
            all_sound_list.append(filez2)

for root, dirs, files in os.walk(path):
    for filez3 in files:
        if filez3.endswith(".avi"):
            avi_list.append(filez3)
            all_sound_list.append(filez3)

for root, dirs, files in os.walk(path):
    for filez4 in files:
        if filez4.endswith(".mov"):
            mov_list.append(filez4)
            all_sound_list.append(filez4)

for root, dirs, files in os.walk(path):
    for filez5 in files:
        if filez5.endswith(".wmv"):
            wmv_list.append(filez5)
            all_sound_list.append(filez5)

for root, dirs, files in os.walk(path):
    for filez6 in files:
        if filez6.endswith(".ogg"):
            ogg_list.append(filez6)
            all_sound_list.append(filez6)

for root, dirs, files in os.walk(path):
    for filez7 in files:
        if filez7.endswith(".wav"):
            wav_list.append(filez7)
            all_sound_list.append(filez7)

print "Searching for sound files..."


objects = ('.MP3', '.MP4', '.AVI', '.MOV', '.WMV', '.OGG', '.WAV')
y_pos = np.arange(len(objects))

if len(all_sound_list) == 0:
    print "No sound files found. Have a good day!"
    sys.exit()

x = raw_input('Press b for a bar chart or t for a table to display the sound files: ')

if x == "b":

    plt.title('Sound Files Bar Chart')

    performance = [len(mp3_list),len(mp4_list),len(avi_list),len(mov_list),len(wmv_list),len(ogg_list), len(wav_list)]
    plt.bar(y_pos, performance, align='center', alpha=0.5)
    plt.xticks(y_pos, objects)
    plt.ylabel('Number of sound files')
    plt.title('Sound File Formats')

elif x == "t":

    fig = plt.title("Sound Files Table")

    data = [[len(mp3_list),len(mp4_list),len(avi_list),len(mov_list),len(wmv_list),len(ogg_list), len(wav_list)]]

    fig, ax = plt.subplots()

    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)

    clust_data = tuple(data)
    collabel=(".MP3", ".MP4", ".AVI", ".MOV", ".WMV", ".OGG", ".WAV")
    ax.table(cellText=clust_data,colLabels=collabel,loc='center')

def show():
    plt.show()

if x == "b":
    thread = threading.Thread(target=show)
    thread.start()

elif x == "t":
    thread = threading.Thread(target=show)
    thread.start()


c = raw_input('Do you want to export to csv?(y/n) ')
if c == 'y':


    output = open('sound files.csv', 'w')
    output_writer = csv.writer(output)
    output_writer.writerow(['MP3 Files:' + ' ' + str(len(mp3_list))])
    output_writer.writerow(['MP4 Files:' + ' ' + str(len(mp4_list))])
    output_writer.writerow(['AVI Files:' + ' ' + str(len(avi_list))])
    output_writer.writerow(['MOV Files:' + ' ' + str(len(mov_list))])
    output_writer.writerow(['WMV Files:' + ' ' + str(len(wmv_list))])
    output_writer.writerow(['OGG Files:' + ' ' + str(len(ogg_list))])
    output_writer.writerow(['WAV Files:' + ' ' + str(len(wav_list))])

    output.close()
    print "Successfully exported sound files to " + str(os.getcwd()) + "\sound files.csv"

elif c == 'n':
    print "Sound files not exported to csv"


print ""
q = raw_input('Do you wish to proceed in remove these sound files?(y/n) ')
if str(q) == "y" or str(q) == "Y":
    q2 = raw_input("WARNING: This will permanently remove all sound files from the selected directory. Are you sure you want to continue?(y/n) ")
    if str(q2) == "y" or str(q2) == "Y":
        for i in range(len(all_sound_list)):
            os.remove(os.path.join(root, all_sound_list[i]))
            if len(all_sound_list) >= 2:
                print 'Successfully deleted sound files.'
            elif len(all_sound_list) < 2:
                print 'Successfully deleted sound file.'
    elif str(q2) == "n" or str(q2) == "N":
        print "Sound files not deleted."
        time.sleep(1)
        sys.exit()
elif str(q) == "n" or str(q) == "N":
    print 'Sound files not deleted.'
    time.sleep(1)
    sys.exit()

sys.exit()


问题是,当我选择表格选项时,它会显示表格图,但也会在单独的窗口中显示条形图。谁能解决这个问题?

也,
我正在尝试提供一个选项,您可以在单个窗口中查看条形图和表格作为子图。能做到吗?如果可以,请提供代码吗?任何帮助表示赞赏,谢谢。

最佳答案

您可以先创建plt.figure(),然后使用ax = plt.subplot(1,2,1)添加子图,其中前两个数字是子图的尺寸,最后一个是当前子图的位置(第二个是plt.subplot(1,2,2))。然后在创建所有子图后执行plt.show()

关于python - matplotlib图的一些错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45222659/

10-12 22:50