gpt4 book ai didi

python - Pyplot/Matplotlib : How to achieve a compressed y-axis

转载 作者:行者123 更新时间:2023-12-01 09:27:15 26 4
gpt4 key购买 nike

我正在使用 pyplot 创建基于 Walter/Lieth 可视化的气候记录仪。

Climograph Walter/Lieth

Another Climograph by Walter/Lieth

正如您在图像(上面的链接)中看到的,右侧 y 轴从值 100 开始压缩。它们的视觉距离变小,而数值间隔变大。

我不知道如何在 pyplot 中实现这一点。我知道如何设置刻度值来创建自定义比例,但当然它们总是等距的。正如您在我的图中所看到的,右侧 y 轴上的绘制空间对应于值的间隔: My plot

也许有人可以提示如何实现上面两个链接中显示的效果。

干杯!

最佳答案

OP 的回答,编辑了他们的问题:

解决方案这是基于阿特森答案的解决方案的示例。缩放函数取自this回答。

from matplotlib import pyplot as plt

def scale(val, src, dst):
"""
Scale the given value from the scale of src to the scale of dst.
"""
return ((val - src[0]) / (src[1]-src[0])) * (dst[1]-dst[0]) + dst[0]

# Actual data
data = [20, 50, 100, 250, 600, 200, 150, 100, 40, 30, 25, 20]

source_scale = (100, 600) # Scale values between 100 and 600
destination_scale = (100, 150) # to a scale between 100 and 150

# Apply scale to all items of data that are above or equal to 100
data_scaled = [x if x < 100 else scale(x, source_scale, destination_scale) for x in data]

# Set up a simple plot
fig = plt.figure()
ax = plt.Axes(fig, [0.,0.,1.,1.])
fig.add_axes(ax)

# Set the y-ticks to a custom scale
ax.set_yticks([0,20,40,60,80,100,110,120,130,140,150])
ax.set_ylim(0, 150)
# Set the labels to the actual values
ax.set_yticklabels(["0","20","40","60","80","100","200","300","400","500","600"])

ax.plot(data_scaled)

Result

关于python - Pyplot/Matplotlib : How to achieve a compressed y-axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50282054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com