【问题标题】:Y axis label in scientific notation when multiple bar charts are plotted绘制多个条形图时,科学计数法的 Y 轴标签
【发布时间】:2021-06-25 11:32:21
【问题描述】:

我试图将多个条形图绘制为子图,但 y 轴不断获取科学记数法值。我运行的初始代码是:

from matplotlib import pyplot as plt
fig, axes = plt.subplots(7,3,figsize = (25, 40)) # axes is a numpy array of pyplot Axes
axes = iter(axes.ravel())  
cat_columns=['Source','Side','State','Timezone',
       'Amenity', 'Bump', 'Crossing', 'Give_Way',
       'Junction', 'No_Exit', 'Railway', 'Roundabout', 'Station', 'Stop',
       'Traffic_Calming', 'Traffic_Signal', 'Turning_Loop', 'Sunrise_Sunset',
       'Civil_Twilight', 'Nautical_Twilight', 'Astronomical_Twilight']
for col in cat_columns: 
    ax = df[col].value_counts().plot(kind='bar',label = col, ax=axes.__next__())  

输出如下所示:

enter image description here

fig, axes = plt.subplots(7,3,figsize = (25, 40)) # axes is a numpy array of pyplot Axes
axes = iter(axes.ravel())  
cat_columns=['Source','Side','State','Timezone',
       'Amenity', 'Bump', 'Crossing', 'Give_Way',
       'Junction', 'No_Exit', 'Railway', 'Roundabout', 'Station', 'Stop',
       'Traffic_Calming', 'Traffic_Signal', 'Turning_Loop', 'Sunrise_Sunset',
       'Civil_Twilight', 'Nautical_Twilight', 'Astronomical_Twilight']
for col in cat_columns: 
    ax = df[col].value_counts().plot(kind='bar',label = col, ax=axes.__next__())
    ax.ticklabel_format(useOffset=False, style='plain') 

使用此行后 ax.ticklabel_format(useOffset=False, style='plain') 我收到如下错误:

enter image description here

请指导我解决这个错误。

【问题讨论】:

    标签: python python-3.x matplotlib bar-chart scientific-notation


    【解决方案1】:

    您可以通过创建自定义 ScalarFormatter 对象并关闭科学记数法来关闭此功能。有关详细信息,请参阅 matplotlib 文档页面 on tick formatters 和 on ScalarFormatter。

    # additional import statement at the top
    from matplotlib import pyplot as plt
    from matplotlib import ticker
    
    # additional code for every axis
    formatter = ticker.ScalarFormatter()
    formatter.set_scientific(False)
    ax.yaxis.set_major_formatter(formatter)
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2013-06-21
      相关资源
      最近更新 更多