【问题标题】:Matplotlib: Thousands separator for y axis [duplicate]Matplotlib:y轴的千位分隔符[重复]
【发布时间】:2022-01-18 15:30:50
【问题描述】:

我创建了一个直方图,一切正常,唯一缺少的是 y 轴的千位分隔符。我怎样才能方便地安装一个?不幸的是,其他记录在案的答案都没有奏效。

# Creating a histogram for order_hour_of_day with titles, labels for the axes and 
#thousands separator (last is still missing)
hist2 = df_final['order_hour_of_day'].plot.hist(bins = 24)
plt.title('Orders per Hour of Day')
plt.xlabel('Hour of Day')
plt.ylabel('Orders')
plt.ticklabel_format(style='plain', axis='y')

谢谢!

【问题讨论】:

  • 不,实际上只是因为我的技能水平让我很困惑,对不起
  • hist2.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter('{x:,.0f}')) 或重复链接中列出的任何其他方法,但删除行 plt.ticklabel_format(style='plain', axis='y')

标签: python pandas matplotlib plot


【解决方案1】:

希望这会有所帮助。它实现了@Mr.T 评论中的答案之一。我使用了 seaborn 的默认数据集“mpg”。

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter, StrMethodFormatter
import seaborn as sns

sns.get_dataset_names() #these are the available example datasets from seaborn
mpg = sns.load_dataset('mpg') 
display(mpg) #shows the dataframe

fig, ax = plt.subplots()
ax.hist = mpg['weight'].plot.hist(bins = 24)
ax.xaxis.set_major_formatter(StrMethodFormatter('{x:,}'))

输出如下所示:

【讨论】:

    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 2012-04-22
    • 2023-03-03
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多