import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] =['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_csv('taiwan4g_copy.csv',encoding='utf8')
# 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()
試了範例的方式一直不能顯示正常,請問大神們幫幫忙,有什麼方向可以指教?
code
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_csv('data.csv')
df2 = df.groupby('縣市')['基地臺'].sum().sort_values(ascending = False)
pd.DataFrame(df2).plot(kind = 'bar', title = '基地臺建設統計')
plt.xlabel('各縣市統計數量')
plt.ylabel('基地臺數量')
result
記得附上 data source
我找 data source 的時間比寫 code 還久..
我用這個
看錯了
我以為你要的效果是跟那張圖一樣
code
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] =['Microsoft JhengHei']
plt.rcParams['axes.unicode_minus'] = False
df = pd.read_csv('data.csv',encoding='utf8')
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