简体   繁体   English

Pyplot,将2个数据集绘制到一个图中,跳过y轴的一部分

[英]Pyplot, plot 2 dataset into one figure, skip part of the y-axis

I am plotting to different datasets into one graph with pylab.plot(), which works great. 我正在使用pylab.plot()将不同的数据集绘制到一张图中,效果很好。 But one dataset has values between 0% an 25% and the other has values between 75% and 100%. 但是一个数据集的值介于0%到25%之间,另一个数据集的值介于75%和100%之间。 I want to skip 30% to 70% on the y-axis to save some space. 我想在y轴上跳过30%到70%,以节省一些空间。 Do you have any suggestions how this might be work with pyplot? 您对pyplot有什么建议吗?

EDIT: 编辑:

For clearness I added the following graphic. 为了清楚起见,我添加了以下图形。 I want to skip 30% to 60% on the y axis, so that the red line and the green line come closer together. 我想在y轴上跳过30%到60%,以便红线和绿线靠得更近。

图像

The solution is based on Space_C0wb0ys post. 该解决方案基于Space_C0wb0ys帖子。

fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot( range(1,10), camean - 25, 'ro-' )
ax.plot( range(1,10), oemean , 'go-' )
ax.plot( range(1,10), hlmean , 'bo-' ) 
ax.set_yticks(range(5, 60, 5))
ax.set_yticklabels(["5","10","15","20","25","30","...","65","70","75"]) 
ax.legend(('ClassificationAccuracy','One-Error','HammingLoss'),loc='upper right')
pylab.show()

This code creates the following graphic. 此代码创建以下图形。 在此处输入图片说明

You could subtract 40 from the x-values for your second functions to make the range of x-values continuous. 您可以从第二个函数的x值中减去40,以使x值的范围连续。 This would give you a range from 0% to 70%. 这将为您提供0%到70%的范围。 Then you can make set the tics and labes of the x-axis as follows: 然后,可以如下设置x轴的抽动和拉贝:

x_ticks = range(71, 0, 10)
a.set_xticks(x_ticks)
a.set_xticklabels([str(x) for x in [0, 10, 20, 30, 70, 80, 90, 100]])

Where a is the current axes. 其中a是当前轴。 So basically, you plot your functions in the range from 0% to 70%, but label the axis with a gap. 因此,基本上,您可以在0%到70%的范围内绘制函数,但要在轴上标注间隙。

To illustrate - the following script: 为了说明-以下脚本:

from numpy import arange
import matplotlib.pyplot as plt

x1 = arange(0, 26) # first function
y1 = x1**2

x2 = arange(75, 100) # second function
y2 = x2*4 + 10

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x1, y1)
ax.plot(x2 - 40, y2) # shift second function 40 to left
ax.set_xticks(range(0, 61, 5)) # set custom x-ticks
# set labels for x-ticks - labels have the gap we want
ax.set_xticklabels([str(x) for x in range(0, 26, 5) + range(70, 101, 5)])
plt.show()

Produces the following plot (note the x-labels): 生成以下图(请注意x标签):

上面脚本的输出

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

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