简体   繁体   English

python 中的 Plot y 轴与 y 值无关

[英]Plot y-axis in python to be independent of y-values

I am a beginner and I am trying to plot some data in python but fail to do so in a satisfying manner.我是初学者,我正在尝试 plot python 中的一些数据,但未能以令人满意的方式做到这一点。 I have a time-series where my y-variable 'alpha' can take 4 different values: 0.0001, 0.001, 0.01 and 0.1.我有一个时间序列,其中我的 y 变量“alpha”可以取 4 个不同的值:0.0001、0.001、0.01 和 0.1。

When I plot this with the regular approach (see below), my y-axis is falsely scaled: I just want these 4 different values for alpha on my y-axis, plotted with the same distance to each other, I do not want the y-scale to reflect their 'true' distances.当我使用常规方法 plot 时(见下文),我的 y 轴被错误地缩放:我只想要 y 轴上的这 4 个不同的 alpha 值,以彼此相同的距离绘制,我不想要y 尺度以反映它们的“真实”距离。 Anyone know how to specify that?有人知道如何指定吗?

Pls find below my code: Thx a lot!:)请在我的代码下面找到:非常感谢!:)

import matplotlib.pyplot as plt
import numpy as np

d1 = {'Year': [1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007], 
'Alpha': [0.0001, 0.1, 0.01, 0.1, 0.0001, 0.001, 0.1, 0.0001, 0.01, 0.001, 0.0001, 0.1]}

df = pd.DataFrame(data=d1)

plt.plot('Year', 'Alpha', data=df, label = "Alpha Value")
plt.show()  

How about this?这个怎么样?

import matplotlib.pyplot as plt
import numpy as np

d1 = {'Year': [1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007], 
'Alpha': [0.0001, 0.1, 0.01, 0.1, 0.0001, 0.001, 0.1, 0.0001, 0.01, 0.001, 0.0001, 0.1]}

df = pd.DataFrame(data=d1)
Alphas = sorted(df.Alpha.unique().tolist())
new_ticks = np.arange(1,len(Alphas)+1)
modict = dict(zip(Alphas, new_ticks))

df.Alpha = df.Alpha.apply(lambda x: modict[x])

plt.plot('Year', 'Alpha', data=df[['Year','Alpha']])
plt.xlabel('Year')
plt.ylabel('Alpha')
plt.yticks(new_ticks, Alphas)
plt.show()  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM