【问题标题】:Pandas plot: Is it possible draw other thing than the column names as label? [duplicate]熊猫图:除了列名之外,是否可以绘制其他东西作为标签? [复制]
【发布时间】:2021-10-13 14:24:47
【问题描述】:

我正在使用这个数据框:https://www.kaggle.com/fivethirtyeight/fivethirtyeight-fandango-dataset 它有几列我想绘制,它们是['RT_norm', 'RT_user_norm', 'Metacritic_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Stars']

当我使用 Pandas 进行任何类型的绘图时,标签是列标签(Duh!)

df.head().plot.bar(x='FILM', y=marc, figsize=(10,8), grid=True)

plt.title('Calificaciones de Películas por Sitio')
plt.ylabel('Calificación')
plt.xlabel('Película')

有没有机会我可以将标签更改为其他内容?我不知道...而不是RT_norm 我想要Rotten Tomatoes Normalized,或者唯一正确的答案是更改数据框中的列名?我尝试使用 yticksylabel 参数,但它们无法按我的意愿工作。

【问题讨论】:

  • 暂时重命名列怎么样?例如df.rename(columns={'RT_norm ': 'Rotten Tomatoes Normalized'}).head().plot.bar(x='FILM', y=marc, figsize=(10,8), grid=True)
  • 嗯,有趣,我忘了如果没有inplace=True不会改变原始数据框,这很聪明。
  • 是的,这个答案也是正确的。有很多方法可以解释一个问题,我从来没有找到一个

标签: python pandas matplotlib


【解决方案1】:

我认为您想使用 plt.legend(labels=..) 更改图例标签:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'FILM':range(100),
                   'y1':np.random.uniform(0,1,100),
                   'y2':np.random.uniform(0,1,100)})
df.head().plot.bar(x='FILM', y=['y1','y2'], figsize=(10,8), grid=True)
plt.legend(labels=['bar1','bar2'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2011-04-13
    • 2019-10-01
    • 1970-01-01
    • 2020-09-08
    相关资源
    最近更新 更多