Issue
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. 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')
Solution
Right now, the data is shown in ones, not millions or hundreds of millions. Notice the 1e8
on the right of the plot. 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. For example, you could use a FuncFormatter
like this:
from matplotlib.ticker import FuncFormatter
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: f'{x * 1e-6:0.1f}'))
Answered By - Mad Physicist
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.