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
natsuki_2002
25.6k21 gold badges49 silver badges51 bronze badges
2 Answers 2
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
tacaswell
88.2k23 gold badges222 silver badges203 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
uhoh
@tcaswell I'm having trouble using
plt.yscale('log') after a plt.scatter() and have described the problem in this question.Aviad Rozenhek
does
ax.set_yscale('log') use np.log or np.log1p for the conversion?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
Marat
15.9k3 gold badges44 silver badges53 bronze badges
2 Comments
user202729
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)Marat
@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.
lang-py