【发布时间】:2017-09-20 05:42:52
【问题描述】:
我正在做一个关于 ICU 数据集的项目,我想为生存和感染制作双条形图。我做到了,它工作正常,因为 Ienter image description here 在这里有它,但现在我想给它一个图例或标签不接受,只是想知道有什么方法可以添加图例并重命名我的图例? 我要红色的表示存活,蓝色的表示感染
matplotlib.pyplot as plt
import numpy as np
import sys
import csv
import pandas as pd
import itertools
from collections import Counter
import pylab as pl
from matplotlib.dates import date2num
import datetime
with open('ICU.csv') as ICU:
#reads csv
df = pd.read_csv(ICU)
#arrays to hold csv data after parsed to int
Survive = []
Infection = []
# parse data column survive to int
for n in df.Survive:
n = int(n)
# adding the converted INT value to the Survive array
Survive.append(n)
# parse data column Infection to int
for n in df.Infection:
n = int(n)
# adding the converted INT value to the Infection array
Infection.append(n)
S = pd.DataFrame.from_dict(Counter(Survive), orient='index')
I = pd.DataFrame.from_dict(Counter(Infection), orient='index')
width = 0.4
fig = plt.figure() # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
S.plot(kind='bar', color='red', ax=ax, width=width, position=1)
I.plot(kind='bar', color='blue', ax=ax, width=width, position=0,
secondary_y=True)
plt.show()
种子
【问题讨论】:
标签: python python-2.7 pandas matplotlib