0

## Pythone annotate text 問題

``````import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] =['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
# print(df)
# print(df[['縣市','基地臺']])
#df_sum = df.groupby('city',as_index=False,sort=False).sum()
df_sum =df.groupby('city')['numbers'].sum().sort_values(ascending=False)
df_sum.plot(x='city', y='numbers',label="基地台數量",kind='bar')
plt.title("基地台建置統計")
plt.xlabel("各縣巿統計數量")
plt.ylabel("基地台數量")
plt.legend(prop = {'size':12})
#plt.ylim(0, 100000)
for p in df_sum.patches:
df_sum.annotate(format(p.get_height(), '.0f'),
(p.get_x() + p.get_width() / 2., p.get_height()),
ha = 'center', va = 'center',
size=15,
xytext = (0, -12),
textcoords = 'offset points')
plt.show()
``````

dragonH iT邦超人 5 級 ‧ 2020-06-26 20:10:33 檢舉

https://qmixfq.by.files.1drv.com/y4msMRf2vsBKNBJTq42L8500w1X9QnePKHwhSlSDFu2HToTkjfsBYmwt195kWP_b8s1oC4gF5pBdqfcVBaX4t2wW5PlQ1StdaQkGK5Po9X0xB3DPS0oGKm318msFwcvDBfCaLUa_msYWxLnkQBkkHjbYoDH_hVq1DH6CgOywowCneTFvV50xbXz8_L5DUdFMGy23UtKzQdVk4My2_Z4CrIzjA/Figure%202020-06-26%20181136.png?psid=1
KYCPG iT邦新手 4 級 ‧ 2020-06-26 20:17:27 檢舉
Import 那行本身在Spyder沒寫錯，漏貼過來論壇了^^"

### 1 個回答

2
dragonH
iT邦超人 5 級 ‧ 2020-06-26 19:51:12

code

``````import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
df2 = df.groupby('縣市')['基地臺'].sum().sort_values(ascending = False)
pd.DataFrame(df2).plot(kind = 'bar', title = '基地臺建設統計')
plt.xlabel('各縣市統計數量')
plt.ylabel('基地臺數量')
``````

result

code

``````import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] =['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
df_sum =df.groupby('縣市')['基地臺'].sum().sort_values(ascending=False)
ax = df_sum.plot(x='縣市', y='基地臺',label="基地台數量",kind='bar')
plt.title("基地台建置統計")
plt.xlabel("各縣巿統計數量")
plt.ylabel("基地台數量")
plt.legend(prop = {'size':12})
for p in ax.patches:
ax.annotate(format(p.get_height(), '.0f'),
(p.get_x() + p.get_width() / 2., p.get_height()),
ha = 'center', va = 'center',
size=6,
xytext = (0, -12),
textcoords = 'offset points')
plt.show()
``````

result

dragonH iT邦超人 5 級 ‧ 2020-06-26 20:05:49 檢舉

vscode 喔

vscode + Python extension + Jupyter

KYCPG iT邦新手 4 級 ‧ 2020-06-26 20:20:47 檢舉

dragonH iT邦超人 5 級 ‧ 2020-06-26 20:53:59 檢舉