简体   繁体   English

使用 pandas 和 matplotlib 从 csv 文件绘制图表

[英]Chart from a csv file using pandas and matplotlib

I have data in the form of dictionaries in the list, generated from the csv file.我在列表中有字典形式的数据,是从 csv 文件生成的。

df = [{'Line Number': 11, 'Report Hour': 6, 'Kits Completed': 34}, {'Line Number': 11, 'Report Hour': 7, 'Kits Completed': 55}, {'Line Number': 12, 'Report Hour': 6, 'Kits Completed': 67}, {'Line Number': 12, 'Report Hour': 7, 'Kits Completed': 56}, {'Line Number': 14, 'Report Hour': 6, 'Kits Completed': 0}, {'Line Number': 14, 'Report Hour': 7, 'Kits Completed': 0}, {'Line Number': 15, 'Report Hour': 6, 'Kits Completed': 123}, {'Line Number': 15, 'Report Hour': 7, 'Kits Completed': 97

To achieve the result shown above, I used the code:为了达到上面显示的结果,我使用了代码:

df = df.to_dict('records')

What interests me is getting the result: line 11: did x (axis) o'clock, y (axis) of boxes.我感兴趣的是得到结果:第 11 行:x(轴)点,y(轴)的盒子。 line 15: did x (axis) o'clock, y (axis) of boxes.第 15 行:x(轴)点,y(轴)的盒子。

Then on the same graph, put the results for lines 14, 15, and so on.然后在同一张图上,将第 14、15 行的结果放在一起,依此类推。 The effect should look something similar to the one below:效果应该类似于下面的效果:

chart example图表示例

I would be grateful for help :)我将不胜感激:)

You probably shouldn't have converted the pandas dataframe to a dictionary, plotting is much easier with pandas:您可能不应该将 Pandas 数据框转换为字典,使用 Pandas 绘图要容易得多:

df = [{'Line Number': 11, 'Report Hour': 6, 'Kits Completed': 34}, {'Line Number': 11, 'Report Hour': 7, 'Kits Completed': 55}, {'Line Number': 12, 'Report Hour': 6, 'Kits Completed': 67}, {'Line Number': 12, 'Report Hour': 7, 'Kits Completed': 56}, {'Line Number': 14, 'Report Hour': 6, 'Kits Completed': 0}, {'Line Number': 14, 'Report Hour': 7, 'Kits Completed': 0}, {'Line Number': 15, 'Report Hour': 6, 'Kits Completed': 123}, {'Line Number': 15, 'Report Hour': 7, 'Kits Completed': 97}]
df = pd.DataFrame(df)

table = df.pivot(index='Report Hour', columns='Line Number', values = 'Kits Completed')
table.plot.bar()

Gives:给出:

条形图

暂无
暂无

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

相关问题 使用 matplotlib 和 CSV 文件中的数据制作条形图 - Making a bar chart by using matplotlib with data from CSV file 使用Pandas DataFrame和Matplotlib将CSV文件中的数据处理到绘图中 - Using pandas dataframe and matplotlib to manipulate data from a csv file into a plot 使用matplotlib和pandas从csv文件绘制直方图 - plotting histogram from csv file using matplotlib and pandas Matplotlib Pyplot饼图从CSV文件 - Matplotlib Pyplot Pie Chart From CSV File 使用来自 csv 的 matplotlib 和 pandas 的 Python 图表不显示所有 x 轴标签 - Python chart using matplotlib and pandas from csv does not show all x-axis labels CSV文件中的新手Matplotlib和Pandas绘图 - Newbie Matplotlib and Pandas Plotting from CSV file 使用pandas和Matplotlib中的csv数据在python中绘制条形图 - Plot bar chart in python using csv data in pandas & Matplotlib 有没有办法使用 Python 中的 matplotlib/pandas 模块根据 csv 文件提供的数据更改条形图的颜色? - Is there a way to change the color of a bar graph based on the data provided from a csv file, using the matplotlib/pandas module in Python? 为什么从带有 Pandas 的 a.CSV 文件中读取的数据在转换为整数后不能使用 matplotlib 绘制? - Why can data read from a .CSV file with Pandas not be plotted using matplotlib after turning it into integers? Pandas DataFrame使用散景或matplotlib的分层饼图/圆环图 - Hierarchic pie/donut chart from Pandas DataFrame using bokeh or matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM