63

In my code, I take the logarithm of two data series and plot them. I would like to change each tick value of the x-axis by raising it to the power of e (anti-log of natural logarithm).

In other words. I want to graph the logarithms of both series but have x-axis in levels.

enter image description here

Here is the code that I'm using.

from pylab import scatter
import pylab
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
file_name = '/Users/joedanger/Desktop/Python/scatter_python.csv'
data = DataFrame(pd.read_csv(file_name))
y = np.log(data['o_value'], dtype='float64')
x = np.log(data['time_diff_day'], dtype='float64')
fig = plt.figure()
plt.scatter(x, y, c='blue', alpha=0.05, edgecolors='none')
fig.suptitle('test title', fontsize=20)
plt.xlabel('time_diff_day', fontsize=18)
plt.ylabel('o_value', fontsize=16)
plt.xticks([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4])
plt.grid(True)
pylab.show()
Trenton McKinney
63.2k41 gold badges170 silver badges214 bronze badges
asked Sep 12, 2013 at 20:21

2 Answers 2

121

let matplotlib take the log for you:

fig = plt.figure()
ax = plt.gca()
ax.scatter(data['o_value'] ,data['time_diff_day'] , c='blue', alpha=0.05, edgecolors='none')
ax.set_yscale('log')
ax.set_xscale('log')

If you are using all the same size and color markers, it is faster to use plot

fig = plt.figure()
ax = plt.gca()
ax.plot(data['o_value'] ,data['time_diff_day'], 'o', c='blue', alpha=0.05, markeredgecolor='none')
ax.set_yscale('log')
ax.set_xscale('log')
answered Sep 12, 2013 at 21:37
Sign up to request clarification or add additional context in comments.

2 Comments

@tcaswell I'm having trouble using plt.yscale('log') after a plt.scatter() and have described the problem in this question.
does ax.set_yscale('log') use np.log or np.log1p for the conversion?
46

The accepted answer is a bit out of date. At least pandas 0.25 natively supports log axes:

# logarithmic X
df.plot.scatter(..., logx=True)
# logarithmic Y
df.plot.scatter(..., logy=True)
# both
df.plot.scatter(..., loglog=True)
answered Oct 13, 2019 at 21:13

2 Comments

It's not really correct to say the other answer is out of date. Note that the question asks for matplotlib which doesn't support logx=, while you're using pandas (which uses matplotlib under the hood, but still)
@user202729 OP is clearly using Pandas ("two data series", code fragment). The accepted answer is not wrong, but there's a newer and better alternative, which dozens of people found helpful. IDK, sounds like out of date to me.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.