I am fairly new to Python visualization. I have data with 3 variables; month, frequency and word. I am trying to see which word appeared most in a month.
import pandas as pd
import seaborn as sns
test = pd.DataFrame({'month': ['2019-01','2019-02','2019-03','2019-04','2019-05'],
'freq':[3,5,22,6,3],
'word':['hello','world','seaborn','seaborn','python']})
sns.barplot(x = 'month', y = 'freq', data = test)
So far, I have month on the x-axis, freq on the y-axis. But, I want to label bars with the words that appear in those months. For example "hello" should appear on Jan-2019 bar.
If I understood you correctly, you can do in this way:
import pandas as pd
import seaborn as sns
test = pd.DataFrame({'month': ['2019-01','2019-02','2019-03','2019-04','2019-05'],
'freq':[3,5,22,6,3],
'word':['hello','world','seaborn','exp','python']})
ax = sns.barplot(x = 'month', y = 'freq', data = test)
for bar, label in zip(ax.patches, test['word']):
x = bar.get_x()
width = bar.get_width()
height = bar.get_height()
ax.text(x+width/2., height + 0.2, label, ha="center")
Use plt.annotate to put the labels in the desired locations.
The ha
and va
keywords take care of proper (horizontal/vertical) alignment.
test = pd.DataFrame({'month': ['2019-01','2019-02','2019-03','2019-04','2019-05'],
'freq':[3,5,22,6,3],
'word':['hello','world','seaborn','seaborn','python']})
for i in test.index:
word = test.loc[i, "word"]
y = test.loc[i, "freq"]
plt.annotate(word, (i, y), ha="center", va="bottom")
sns.barplot(x = 'month', y = 'freq', data = test);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.