简体   繁体   English

如何更改 Matplotlib 条形图上 x 轴标签上显示的单位

[英]How do I change the units shown on the x-axis labels on a Matplotlib bar chart

I'm trying to make it so the ticks on the x-axis for revenue show the value as a factor of a million rather than as a factor of a hundred million as they are now.我正在努力使收入的 x 轴上的刻度显示价值为百万倍,而不是像现在这样的一亿倍。 I can't seem to figure out how to accomplish this.我似乎无法弄清楚如何实现这一点。 My code and the resulting bar chart is below.我的代码和生成的条形图如下。

import numpy as np
import pandas as pd
import matplotlib.ticker as plticker
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display
from pandas import Series

%matplotlib inline


# Define Figure Size
fig, ax = plt.subplots(figsize=(25,25))

# Get the average vote_average for each genre
average_revenue = df2.groupby('release_year')['revenue_adj'].mean()

# Find positions of y ticks
y_pos = np.arange(len(df2['release_year'].unique()))
# Set up Bar Chart
ax.set_yticks(y_pos)
ax.set_yticklabels(sorted(df2['release_year'].unique()))
ax.set_xlabel('Revenue in Millions', fontsize=16)
ax.set_ylabel('Release Year', fontsize=16)
ax.set_title('Revenue by Release Year', fontsize=20)

# Set Size of X and Y labels
plt.rc('xtick', labelsize=14)
plt.rc('ytick', labelsize=14)

# Put Values next to Each Bar
for i, v in enumerate(average_revenue):
    a = v/1000000
    ax.text(v, i, ('$' + str(round(a,2)) + 'M'), color='blue')
    
ax.invert_yaxis()  # labels read top-to-bottom

# Draw Bar Chart
ax.barh(y_pos, average_revenue, align='center', color='green', ecolor='black')

Python条形图

Right now, the data is shown in ones, not millions or hundreds of millions.现在,数据以单位显示,而不是数百万或数亿。 Notice the 1e8 on the right of the plot.注意图右侧的1e8 You can plot the value in millions by dividing the input by a million:您可以通过将输入除以百万来绘制以百万为单位的值:

ax.barh(y_pos, average_revenue * 1e-6, ...)

Alternatively, you can adjust the formatter on the x-axis if you prefer not to alter the data.或者,如果您不想更改数据,您可以在 x 轴上调整格式化程序 For example, you could use a FuncFormatter like this:例如,您可以像这样使用FuncFormatter

from matplotlib.ticker import FuncFormatter

ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: f'{x * 1e-6:0.1f}'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何挤压 matplotlib 条形 x 轴标签以删除缺失的数值? - How do I squeeze matplotlib bar x-axis labels to drop missing numerical values? Matplotlib 条形图 X 轴标签顺序 - Matplotlib bar chart X-axis Labels order 如何在Matplotlib中交错或偏移x轴标签? - How do I stagger or offset x-axis labels in Matplotlib? 如何旋转Matplotlib条形图上的X轴刻度标签? 尝试了几种方法,都没有用 - How to rotate x-axis tick labels on matplotlib bar chart? Tried several approaches, none worked 如何使用 x 轴上的时间值更改条形图中的 x 轴刻度/刻度? - How do you change the x-axis scale/ticks in a bar chart with time values in the x-axis? 如何使用 matplotlib 在 x 轴条形图上插入标签? - How to insert labels on to x-axis bar graph using matplotlib? 如何使用 matplotlib 中的日期时间更改 x 轴的范围? - How do I change the range of the x-axis with datetimes in matplotlib? 如何更改 x 轴上的数字(matplotlib) - How do I change the number on x-axis (matplotlib) 为什么我执行“ log”操作时,matplotlib条形图会压缩x轴 - Why is my matplotlib bar chart compressing the x-axis when I do 'log' matplotlib当x轴相隔1周时,如何减少堆积条形图中条形之间的空间量? - matplotlib how do I reduce the amount of space between bars in a stacked bar chart when x-axis are dates 1-week apart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM