看板 Python 關於我們 聯絡資訊
各位大大好, 小妹正在練習用matplotlib. Pyplot 練習畫群組直條圖,但發現y軸數字太長,單位變成「le7」,請問各位大大有無辦法講單位改成千位計算,並且標記單位「$k」呢? # 資料表/圖: https://imgur.com/a/xqhpTwj # 程式: import pandas as pd import matplotlib.pyplot as plt product_rev=pd.read_excel("revenue.xlsx") product_rev.sort_values(by="2017",inplace=True,ascending=False) print(product_rev) product_rev.plot.bar(x="Product",y=["2016","2017"],color=["blue","orange"]) plt.title("Total revenue by products",fontsize=16,fontweight="bold") plt.xlabel("Product") plt.ylabel("Figure") plt.tight_layout() plt.show() ----- Sent from JPTT on my iPhone -- Hakuna Matata -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.9.98.23 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1561476452.A.82C.html ※ 編輯: treefrog (101.9.98.23 臺灣), 06/25/2019 23:33:15
WayneHong: 感覺直接改原始資料比較快 06/26 08:28
yuasa: 先用product_rev.div(1000),把妳的原始資料都除1000。再改 06/26 12:29
yuasa: 你ylabel的單位就好了 06/26 12:31
treefrog: 不好意思,請問怎麼修改單位? 06/26 12:37
gmccntzx1: google “matplotlib FuncFormatter” 06/26 12:42
感謝各位大大解答,後來有找到方式解決,分享給有一樣問題的捧友 :) # 修改之程式碼: import pandas as pd from pandas import read_excel import matplotlib.pyplot as plt product_rev=read_excel("revenue.xlsx") product_rev["2016"]=product_rev["2016"].div(1000) #數字除以1,000 product_rev["2017"]=product_rev["2017"].div(1000) #數字除以1,000 product_rev.sort_values(by="2017",inplace=True,ascending=False) print(product_rev) ax = product_rev.plot.bar(x="Product",y=["2016","2017"],color=["blue","orange"]) plt.title("Total revenue by products",fontsize=16,fontweight="bold") plt.xlabel("Product") plt.ylabel("Figure") # 加上單位 $K ax.ticklabel_format(axis='y', style='plain') plt.text(-0.03,1.07,'($K)', horizontalalignment='center',verticalalignment='center', transform=ax.transAxes) # 將 X軸名稱旋轉 45度 ax1=plt.gca() ax1.set_xticklabels(product_rev["Product"],rotation="45",ha="right") plt.tight_layout() plt.show() #產生 Bar chart如下:(以千為單位計算,標記單位為$K) https://imgur.com/a/nxAP3nG ※ 編輯: treefrog (101.9.98.23 臺灣), 06/26/2019 15:45:09
Pieteacher: Try plt.semilogy() 06/26 18:54