如何在条形图中清楚地显示每个数据的标题?

时间:2018-09-16 14:29:07

标签: python pandas matplotlib plot

我有一个数组

a  = ['Film & Video', 'Fashion', 'Art', 'Games', 'Music', 'Publishing', 'Technology', 'Music', 'Theater',.....'Dance', 'Art', 'Film & Video']

我用熊猫对数组中的字符串进行频率计数以绘制图

import pandas
from collections import Counter
import matplotlib.pyplot as plt

letter_counts = Counter(a)
df = pandas.DataFrame.from_dict(letter_counts, orient='index')
df.plot(kind='bar')
plt.xlabel('Category')
plt.ylabel('Counts')
plt.title("Successful Categories")

plt.show()

enter image description here

您看不到我的x轴标题或长字符串(例如“电影和视频”)的全名。我正在寻找一种更好地可视化我的情节的方法。另外,如何删除或编辑图表右上方的框?

1 个答案:

答案 0 :(得分:2)

以下是通过旋转刻度线标签(有助于减小标签跨越的垂直高度),隐藏图例并使用紧凑布局的一种轻微修改方法。我创建了一个示例数据,并使用了以下几行(仅部分显示了代码)。我将&替换为and

letter_counts = Counter(a)
df = pandas.DataFrame.from_dict(letter_counts, orient='index',columns=['values'])
df.plot.bar(y='values', legend=False)
plt.xticks(rotation=45)
plt.tight_layout()

输出

enter image description here