python:散点图对数刻度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18773662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:47:49  来源:igfitidea点击:

python: scatter plot logarithmic scale

pythonmatplotliblogarithm

提问by natsuki_2002

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).

在我的代码中,我取两个数据系列的对数并绘制它们。我想通过将 x 轴的每个刻度值提高到 e(自然对数的反对数)的幂来更改它。

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

换句话说。我想绘制两个系列的对数,但在级别中有 x 轴。

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()

采纳答案by tacaswell

let matplotlibtake the log for you:

让我们matplotlib为您记录日志:

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

如果您使用所有相同的尺寸和颜色标记,使用速度会更快 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')

回答by Marat

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

接受的答案有点过时了。至少 pandas 0.25 本身支持对数轴:

# logarithmic X
df.plot.scatter(..., logx=True)
# logarithmic Y
df.plot.scatter(..., logy=True)
# both
df.plot.scatter(..., loglog=True)