MatPlotLib Dollar Sign with Thousands Comma Tick Labels

后端 未结 1 1686
情歌与酒
情歌与酒 2020-12-06 01:43

Given the following bar chart:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({\'A\': [\'A\', \'B\'], \'B\': [1000         


        
相关标签:
1条回答
  • 2020-12-06 02:10

    You can use StrMethodFormatter, which uses the str.format() specification mini-language.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.ticker as mtick
    
    df = pd.DataFrame({'A': ['A', 'B'], 'B': [1000,2000]})
    
    fig, ax = plt.subplots(1, 1, figsize=(2, 2))
    df.plot(kind='bar', x='A', y='B',
            align='center', width=.5, edgecolor='none', 
            color='grey', ax=ax)
    
    fmt = '${x:,.0f}'
    tick = mtick.StrMethodFormatter(fmt)
    ax.yaxis.set_major_formatter(tick) 
    plt.xticks(rotation=25)
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题