简体   繁体   English

使用 matplotlib 和 CSV 文件中的数据制作条形图

[英]Making a bar chart by using matplotlib with data from CSV file

As it have been written in the title,i do not have an idea how to make a bar chart with data from CSV file because i am just started to programming in Python.正如标题中所写,我不知道如何使用 CSV 文件中的数据制作条形图,因为我刚刚开始在 Python 中编程。

My CSV file looks like this:我的 CSV 文件如下所示:

CSV CSV

You can use pandas to read your csv file, then matplotlib to plot the bar chart.您可以使用pandas读取您的 csv 文件,然后使用matplotlib读取 Z32FA6E1B78A9D4028953E6AAZA 条形图。 As an example:举个例子:

import pandas as pd
import matplotlib.pyplot as plt

filename = <path/to/your/csv/file>
df = pd.read_csv(filename, sep=',', header=0)

fig = plt.figure()

x = df['Characters']
y = df['Number of occurrences']

width = 1.0

plt.bar(x, y, width, color='b' )
plt.show()
import csv
import matplotlib.pyplot as plt
import numpy as np

keys = []
data = []
with open('csv_file.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        keys.append(row[0])
        data.append(row[1])

x_idx = np.arange(len(keys))
plt.bar(x_idx, data, align='center', alpha=0.5)
plt.xticks(x_idx, keys)
plt.ylabel('Data')
plt.title('Your title')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM