matplotlib.pyplot - 放大y轴

时间:2018-02-28 17:12:19

标签: python matplotlib

我使用一些python代码来创建以下boxplot。 boxplot

然而,由于第二个框非常小,我想放大第一个y-tick(0)和第二个y-tick(20)之间的部分以便进行放大。这可能是蟒蛇?我很高兴有任何建议或帮助。我已经找到了任何可能的解决方案,但我还没有找到完美的功能(虽然我认为它有一个日志功能,但无法使其正常运行)。

这是代码,我用来创建boxplot:

import matplotlib.pyplot as plt
import pandas as pd
import itertools
from matplotlib.patches import Polygon 
import numpy as np

data_1 = [75, 85, 72]
tuple_1 = tuple(data_1) 

data_2 = [1, 2, 1.5] 
tuple_2 = tuple(data_2)

data = [tuple_1, tuple_2]
numDists=2
randomDists = ['tuple_1', 'tuple_2']    
fig, ax1 = plt.subplots()
fig.canvas.set_window_title('A Boxplot Example')
plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.5)
bp = plt.boxplot(data, notch=0, sym='+', vert=1, whis=1.5)
plt.setp(bp['boxes'], color='black')
plt.setp(bp['whiskers'], color='black')
plt.setp(bp['fliers'], color='black', marker='+')
ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey',
           alpha=0.5)
ax1.set_axisbelow(True)
ax1.set_title('')
ax1.set_xlabel('x-axis')
ax1.set_ylabel('y-axis')
boxColors = ['red', 'blue']
numBoxes = numDists
medians = list(range(numBoxes))
for i in range(numBoxes):
    box = bp['boxes'][i]
    boxX = []
    boxY = []
    for j in range(5):
        boxX.append(box.get_xdata()[j])
        boxY.append(box.get_ydata()[j])
    boxCoords = list(zip(boxX, boxY))
    k = i % 2
    boxPolygon = Polygon(boxCoords, facecolor=boxColors[k])
    ax1.add_patch(boxPolygon)
    med = bp['medians'][i]
    medianX = []
    medianY = []
    for j in range(2):
        medianX.append(med.get_xdata()[j])
        medianY.append(med.get_ydata()[j])
        plt.plot(medianX, medianY, 'k')
        medians[i] = medianY[0]

    plt.plot([np.average(med.get_xdata())], [np.average(data[i])],
         color='w', marker='*', markeredgecolor='k')

ax1.set_xlim(0.5, numBoxes + 0.5)
top = 110
bottom = 0
ax1.set_ylim(bottom, top)
xtickNames = plt.setp(ax1, xticklabels=np.repeat(randomDists, 1))
plt.setp(xtickNames, rotation=45, fontsize=8)

pos = np.arange(numBoxes) + 1
upperLabels = [str(np.round(s, 2)) for s in medians]
weights = ['bold', 'semibold']
for tick, label in zip(range(numBoxes), ax1.get_xticklabels()):
    k = tick % 2
    ax1.text(pos[tick], top - (top*0.05), upperLabels[tick],
         horizontalalignment='center', size='x-small', weight=weights[k],
         color=boxColors[k])
plt.tight_layout()
plt.savefig("test.png")
plt.show()

0 个答案:

没有答案