Matplotlib组合数据点

ss2ws0br  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(166)

我有这个dataframe:

我想把电影按顺序排列,但当我尝试时,它看起来是这样的:

如何在不合并列出两次的影片的情况下绘制此数据框?(即-“壮志凌云:小牛”和“阿凡达”:水之路)

jm2pwxwz

jm2pwxwz1#

你可以在Pandas中使用barh

data2 = pd.DataFrame({'Film': ['Top Gun: Maverick', 'The Batman', 'Top Gun: Maverick'],
                      'Weekend_gross': [1, 2, 3]})

data2 = data2.sort_values('Weekend_gross')

clrs = ['r', 'orange', 'chartreuse']

fig, ax = plt.subplots(figsize=(10, 7))
data2.plot.barh('Film', 'Weekend_gross', color=clrs, legend=False, title='What movie Grossed the Most in 1 Weekend', ax=ax)
plt.tight_layout()
plt.show()

输出:

thigvfpy

thigvfpy2#

您可以在ax.barh内部传递y,而不是将data2['Film]传递给ax.barh,而是传递一系列连续数字,然后使用ax.set_yticksdata2['Film']的值设置为yticks。例如:

import pandas as pd
import matplotlib.pyplot as plt

data2 = pd.DataFrame({'Film': ['Film A', 'Film B', 'Film A', 'Film D'],
                      'Weekend_gross': [1, 3, 7, 5]})

data2 = data2.sort_values('Weekend_gross')

clrs = ['teal', 'orange', 'cyan', 'violet']

fig, ax = plt.subplots(figsize=(10, 7))

# create a range with length of your data
idx = range(len(data2))

# pass `idx` to `y` param
ax.barh(idx, data2['Weekend_gross'], 
        color=clrs, edgecolor='black')
ax.set(title='What movie Grossed the Most in 1 Weekend', 
       ylabel='Film')

# set `yticks` with `Film` column
ax.set_yticks(idx, data2['Film'])
plt.show()

结果:

当然,如果您不介意重置索引,您可以使用data2.index来实现这一点。例如:

# reset index:
data2 = data2.sort_values('Weekend_gross').reset_index(drop=True)

...
# pass as `y`
ax.barh(data2.index, data2['Weekend_gross'], 
        color=clrs, edgecolor='black')

...
# and set yticks
ax.set_yticks(data2.index, data2['Film'])

相关问题