【问题标题】:Multiple Labels for Counter() Data Frame Frame Objects in Matplotlib Python?Matplotlib Python中Counter()数据框框架对象的多个标签?
【发布时间】: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


    【解决方案1】:

    你可以使用

    plt.legend(labels=("survive", "infection"))
    

    【讨论】:

    • 感谢您的回答。我已经尝试过了,但没有用,只是将标签放在彼此的顶部,没有任何其他更改
    • 也许我当时没有很好地理解你的问题。这也是由于您没有提供minimal reproducible example 造成的。但你至少应该尝试更好地描述你想要什么、你得到什么以及在多大程度上不符合你的期望。
    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2018-01-22
    • 2018-04-29
    • 2021-11-13
    • 2011-05-07
    相关资源
    最近更新 更多