【问题标题】:Python - data displaying incorrectly (Matplotlib)Python - 数据显示不正确(Matplotlib)
【发布时间】:2017-05-30 22:36:50
【问题描述】:

我是 python 的新手,我有一个问题。我正在从 CSV 文件加载数据,删除重复项,保存删除的重复项 csv 文件,然后加载正确的 CSV 文件并生成图表。然而,我的问题是,图表没有正确显示,因为总数是错误的。我知道程序工作正常,因为如果我删除第 1 节(请参阅下面的#section1),我会得到正确的数据显示。我看不出第 1 节中的数据会扭曲数据......任何帮助将不胜感激。谢谢。

总结:不能在同一个 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”对于这个问题都是无用的。相反,尝试创建一个minimal reproducible example,仅使用 csv 文件中的几行数据。尝试将“section1”的输出与您的期望进行比较。此外,清楚地说明您对问题的期望,并更正上面的代码,以便在 for 循环中缩进。
  • 这整个程序就是完整的程序,我想说的是,如果我从 .py 文件中删除第 1 节,我会得到显示正确计数的图表。当我包括第 1 节时,如果这有意义的话,我会得到更少的计数。例如,饼图上显示的手持浏览器的计数为 221,但正确的数字是 240,如果我从脚本中删除第一部分,我会得到正确的数字,但我在一个脚本中都喜欢它们。因此,出于某种原因,第 1 部分正在扭曲计数

标签: python numpy matplotlib


【解决方案1】:

解决方案是在每次创建重复数据库之后添加“del writer”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 2021-10-07
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    相关资源
    最近更新 更多