0

enter image description here I want something similar to the above image where the chart is a log distribution of the data but the x-axis ticks are original data values spaced accordingly.

enter image description here This is what I have currently.

I tried ideas here. But they all seem to be manually setting tick values, I want the tick values to be derived from the original column values.

My current code is very basic:

sns.histplot(data=df, x="MPG",log_scale=True)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Can you expand a bit on "but the x-axis ticks are original data values spaced accordingly" ? It's unclear to me. Also, can you provide a sample of data? You do want the x log scale, but you want to change the tick labels? – thmslmr Mar 07 '23 at 08:57
  • @thmslmr I've edited the question. You are right, I want the x axis to be log scale, but want the tick labels to reflect the original values, to make the chart more readable. – learning_new Mar 07 '23 at 10:29

2 Answers2

0

If I understand correctly, you have something like this:

X = np.random.normal(loc=50, scale=10, size=1000)

fig, ax = plt.subplots(figsize=(10, 6))
ax.hist(X, range(20, 90, 5), ec="k")

enter image description here

And would like to pass it to log scale, while keeping the ticks values and labels (but on the log scale then).

If this is what you're trying to achieve, you should apply the log scale on the X axis with ax.set_xscale, and set the ticks values and labels with ax.set_xticks

fig, ax = plt.subplots(figsize=(10, 6))
ax.hist(X, range(20, 90, 5), ec="k")

ax.set_xscale("log")

ticks_range = range(20, 90, 10)  # The ticks values you want here.
ax.set_xticks(ticks_range, ticks_range)

enter image description here

thmslmr
  • 1,251
  • 1
  • 5
  • 11
0

If I understand correctly, you want to show a histogram of the log of the values:

from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np

df = sns.load_dataset('mpg')

df['log mpg'] = np.log10(df['mpg'])
ax = sns.histplot(data=df, x="log mpg")

plt.tight_layout()
plt.show()

sns.histogram of log of the values

Or: ax = sns.histplot(data=np.log(df["mpg"])) if you don't want to add a column to the dataframe, and don't need other seaborn features such as hue.

JohanC
  • 71,591
  • 8
  • 33
  • 66