-3
我是Python的新手,我有一個問題。我從CSV文件加載數據,刪除重複項,保存已刪除的複製csv文件,然後加載正確的CSV文件並生成圖形。然而,我的問題是,圖表沒有正確顯示,因爲總數是錯誤的。我知道該程序工作正常,因爲如果我刪除第1部分(請參閱下面的#section1),我得到正確的數據顯示。我無法看到第1部分中的內容會導致數據偏差......任何幫助都將不勝感激。謝謝。Python - 數據顯示不正確(Matplotlib)
摘要:無法在同一個py文件中運行第1部分和第2部分,否則數據會錯誤地計入。想知道爲什麼?如何避免它而不運行單獨的py文件。
from collections import Counter
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import csv
import itertools
第1
# Create database of duplicates - check if the mac and os pairs have duplicates
reader = csv.reader(open('Workbook1.csv', 'r'), delimiter=',')
writer = csv.writer(open('remacos.csv', 'w'), delimiter=',')
entries = set()
for row in reader:
key = (row[1], row[2])
if key not in entries:
writer.writerow(row)
entries.add(key)
entries.clear()
# Create database of duplicates - check if the mac and browser pairs have duplicates
reader = csv.reader(open('Workbook1.csv', 'r'), delimiter=',')
writer = csv.writer(open('remacbrowser.csv', 'w'), delimiter=',')
entries = set()
for row in reader:
key = (row[1], row[3])
if key not in entries:
writer.writerow(row)
entries.add(key)
第2節
# Read Removed Duplicated entries Database and Count Values for OS.
df = pd.read_csv('remacos.csv', index_col="mac")
counteros = Counter(df['os'])
os_names = counteros.keys()
os_counts = counteros.values()
# Read Removed Duplicated entries Database and Count Values for Browsers.
df = pd.read_csv('remacbrowser.csv', index_col="mac")
counterbrowsers = Counter(df['browser'])
browser_names = counterbrowsers.keys()
browser_counts = counterbrowsers.values()
創建2個條形圖和餅圖
# Plot histogram using matplotlib bar() for OS.
indexes = np.arange(len(os_names))
width = 0.7
plt.bar(indexes, os_counts, width)
plt.xticks(indexes + width * 0.5, os_names)
plt.show()
# Plot histogram using matplotlib bar() for Browsers.
indexes = np.arange(len(browser_names))
width = 0.7
plt.bar(indexes, browser_counts, width)
plt.xticks(indexes + width * 0.5, browser_names)
plt.show()
# Make Pie Chart for OS's
plt.figure()
values = os_counts
labels = os_names
def make_autopct(values):
def my_autopct(pct):
total = sum(values)
val = int(round(pct*total/100.0))
return '{p:.2f}% ({v:d})'.format(p=pct,v=val)
return my_autopct
plt.pie(values, labels=labels, autopct=make_autopct(values))
#plt.pie(values, labels=labels) #autopct??
plt.show()
# Make Pie Chart for Browsers
plt.figure()
values = browser_counts
labels = browser_names
def make_autopct(values):
def my_autopct(pct):
total = sum(values)
val = int(round(pct*total/100.0))
return '{p:.2f}% ({v:d})'.format(p=pct,v=val)
return my_autopct
plt.pie(values, labels=labels, autopct=make_autopct(values))
#plt.pie(values, labels=labels) #autopct??
plt.show()'
「圖表沒有被正確顯示」不是一個足夠的問題描述。正如你所說的,在使用原始文件時,繪圖效果很好,問題與「section1」有關,因此所有的「section2」對這個問題都沒有用處。相反,嘗試使用csv文件中的幾條數據線來創建[MCVE]。嘗試比較「section1」的輸出和你的期望。此外,清楚地說明您期望的問題,並更正for循環中的縮進代碼。 – ImportanceOfBeingErnest
這整個事情是完整的程序,我想說的是,如果我從.py文件中刪除第1部分,我會得到顯示正確計數的圖表。當我包括第1部分時,如果這有意義,我可以得到更低的計數。例如,當餅圖上的displayd爲221時HandHeld瀏覽器的計數爲240,但如果我從腳本中刪除第一部分,但在同一個腳本中同樣喜歡它們,則會得到正確的數字。所以出於某種原因,第1部分正在歪曲計數 – user2273231