首页 > 解决方案 > 使用 Matplotlib 从具有多列的 CSV 文件中绘制图形

问题描述

我正在尝试boxplot从这样设置的 csv 文件中创建一个 x 标签为公司(第 5 列)和 y 标签为总体评级(第 1 列)的标签:

4,Missing,Missing,No opinion of CEO,ey,3.5,,3,4,3,2008,Current 
Employee,auditor,miami,Missing,unknown,audit
5,Recommends,Missing,Approves of CEO,ey,5,,5,5,5,2008,Current 
Employee,tax,tampa,Missing,unknown,tax
5,Recommends,Missing,Approves of CEO,ey,5,,5,4,5,2008,Current 
Employee,audit staff,tampa,Missing,associate,audit

从中,我想提取整体评级列和公司列以将其绘制在箱线图上。这是我到目前为止尝试过的代码,但没有奏效:

import matplotlib.pyplot as plt
import csv
x=[]
y=[]
with open('Ratings.csv', 'r') as csvfile:
    bplot1 = axes[0].boxplot(all_data,vert=True,patch_artist=True)
    bplot2 = axes[1].boxplot(all_data,vert=True,patch_artist=True)
colors = ['pink', 'lightblue', 'lightgreen']
for bplot in (bplot1, bplot2):
    for patch, color in zip(bplot['boxes'], colors):
        patch.set_facecolor(color)
    plt.plot(x,y, marker='o')
plt.title('Overall Rating Based on Firm')
plt.xlabel('Firm')
plt.ylabel('Overall Rating')
plt.show()

标签: python

解决方案


推荐阅读