magis_virtute
magis_virtute

Reputation: 1

How to change the axis scale of a boxplot in seaborn using a dataframe with pandas?

I have created a seaborn boxplot graph for investment data ranging from $100,000 to $40,000,000, spread out over several years. The graph shows a boxplot for investment amounts per year. The data comes from two series in a dataframe, which I'm rendering from a CSV: investment year ('inv_year') and investment amount ('inv_amount').

The problem: the graph's y-axis is automatically shown in increments of 0.5, so:

0.0, 0.5, 1.0, 1.5, etc. (i.e. in tens of millions)

but I want the axis in increments of 5, so:

0, 5, 10, 15, etc. (i.e. in millions)

How do I change the axis's scale?

Here's my current code:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_excel('investments'))
plt.figure(figsize=(12, 10))
sns.boxplot(x='inv_year', y='inv_amount', data=df)
plt.show()

Upvotes: 0

Views: 2707

Answers (1)

runDOSrun
runDOSrun

Reputation: 11005

You can define an array with the specified ticks and pass it to plt.yticks:

plt.yticks(np.arange(min(x), max(x)+1, 5))

If you want to keep the limits but just adjust the ticks:

increments = 5
ax = plt.gca()
start, end = ax.get_ylim()
ax.yaxis.set_ticks(np.arange(start, end, increments))

Upvotes: 1

Related Questions