Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: scatter plot logarithmic scale

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() 
like image 993
natsuki_2002 Avatar asked Sep 12 '13 20:09

natsuki_2002


People also ask

How do you plot a log scale in Python?

pyplot library can be used to change the y-axis or x-axis scale to logarithmic respectively. The method yscale() or xscale() takes a single value as a parameter which is the type of conversion of the scale, to convert axes to logarithmic scale we pass the “log” keyword or the matplotlib. scale.

What is meant by logarithmic scale?

Definition of logarithmic scale : a scale on which the actual distance of a point from the scale's zero is proportional to the logarithm of the corresponding scale number rather than to the number itself — compare arithmetic scale.


2 Answers

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') 
like image 129
tacaswell Avatar answered Sep 27 '22 19:09

tacaswell


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) 
like image 39
Marat Avatar answered Sep 27 '22 19:09

Marat