Max Pyziur
Max Pyziur

Reputation: 11

Python - MatPlotLib.PyPlot - Removing tick marks from Right Axis

I have the following pyplot subroutine. All data passes correctly to the subroutine. However, I am not able to control two things: 1 - I can't make the right ticks disappear 2 - I can't place a 2nd y-label on the right axis

I've added an example image which has right-ticks that I'm trying to remove, and also trying to add a right-hand y-axis label.

Much thanks!


def makeChart(dataToGraph,  titleForGraph, titleYlabel, titleYlabel2, legendLabels, yMax, yMin, dataSource):

    fig, ax = plt.subplots()

    colorZ2 = ('#002060', 'red') 
    colorZ1 = ( '#92D050', 'purple')
    p1 = plt.Rectangle((0, 0), 1, 1, fc='red')
    p2 = plt.Rectangle((0, 0), 1, 1, fc='#002060')
    p3 = plt.Rectangle((0, 0), 1, 0, fc='black')
    p4 = plt.Rectangle((0, 0), 1, 1, fc='#92D050')
    p5 = plt.Rectangle((0, 0), 1, 1, fc='purple')

#   plt.ylim(yMin, yMax)

    ax2 = ax.twinx()
    ax2 = ax.twiny()

    ax3 = ax.twinx()
    ax3 = ax.twiny()

    dates, series1, series2, series3, series4, series5 = zip(*dataToGraph)

    ax.stackplot(dates, series1, series2, colors=colorZ1)
    ax2.stackplot(dates, series3, series4, colors=colorZ2)
    ax3.plot(dates, series5, c='black', lw=3)

    ax2.tick_params(labeltop='off', labelright='off')  # labelright is not working
    ax3.tick_params(labeltop='off', labelright='off')  # labelright is not working

# How do you turn off ticks on the right ticks?

    ax3.legend([p5, p4, p3, p2, p1 ], [legendLabels[4],legendLabels[3],legendLabels[2],legendLabels[1],legendLabels[0]], loc='best', shadow=True)

    plt.title('\n'.join(wrap(titleForGraph,50)), fontsize=14)

    ax.set_ylabel(titleYlabel, fontsize=14)

# How do you get the label for the right to appear

    ax2.set_ylabel(titleYlabel2, fontsize=14) # not working
    ax3.set_ylabel(titleYlabel2, fontsize=14) # not working

    y_format = tkr.FuncFormatter(numFunc)
    ax.yaxis.set_major_formatter(y_format)

    props = dict(boxstyle='square', facecolor='none' )
    fig.text(.999, 0.01, 'EPRINC', style='oblique', ha='right', fontsize=10, bbox=props)
    fig.text(.001, 0.01, dataSource, style='oblique', ha='left', fontsize=10, bbox=props)
    ax.grid(True)
    fig.autofmt_xdate()

    plt.show()


def numFunc(x, pos):  # formatter function takes tick label and tick position
    s = '{:0,d}'.format(int(x))
    return s

enter image description here

Upvotes: 0

Views: 4318

Answers (3)

Max Pyziur
Max Pyziur

Reputation: 11

Ok, I'm learning the mechanics of stackoverflow.com. I'm including the matplotlib subroutines, and an example of the output as I had hoped to achieve it. def makeChart(dataToGraph, titleForGraph, titleYlabel, titleYlabel2, legendLabels, yMax, yMin, dataSource):

fig, ax = plt.subplots()
ax2 = ax.twinx()

colorZ2 = ('#002060', 'red') 
colorZ1 = ( '#92D050', 'purple')
p1 = plt.Rectangle((0, 0), 1, 1, fc='red')
p2 = plt.Rectangle((0, 0), 1, 1, fc='#002060')
p3 = plt.Line2D([], [], color='black', linewidth=4)
p4 = plt.Rectangle((0, 0), 1, 1, fc='#92D050')
p5 = plt.Rectangle((0, 0), 1, 1, fc='purple')


dates, series1, series2, series3, series4, series5 = zip(*dataToGraph)

ax.stackplot(dates, series1, series2, colors=colorZ1)
ax.stackplot(dates, series3, series4, colors=colorZ2)
ax.plot(dates, series5, c='black', lw=3)

ax.set_ylabel(titleYlabel, fontsize=14)
ax2.tick_params(labelright='off')  
ax2.set_ylabel(titleYlabel2, fontsize=14) 


ax.legend([p5, p4, p3, p2, p1 ], [legendLabels[4],legendLabels[3],legendLabels[2],legendLabels[1],legendLabels[0]], loc='upper left', shadow=True)

plt.title('\n'.join(wrap(titleForGraph,50)), fontsize=14)

y_format = tkr.FuncFormatter(numFunc)
ax.yaxis.set_major_formatter(y_format)

props = dict(boxstyle='square', facecolor='none' )
fig.text(.999, 0.01, 'EPRINC', style='oblique', ha='right', fontsize=10, bbox=props)
fig.text(.001, 0.01, dataSource, style='oblique', ha='left', fontsize=10, bbox=props)
ax.grid(True)
fig.autofmt_xdate()

plt.show()

def numFunc(x, pos): # formatter function takes tick label and tick position s = '{:0,d}'.format(int(x)) return s

Not sure where the image went; but it's a supply/demand balance view of the U.S. Residual fuel market

Upvotes: 0

Max Pyziur
Max Pyziur

Reputation: 11

Well, a little more tinkering, and now I'm getting the results that I want. I removed the ax2 = twiny(), and am now using ax2 = twinx().

I can post the code to illustrate, if there is an interest.

Thank you again for your help.

Upvotes: 0

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339765

You're creating 4 twin axes. And you directly loose the reference to 2 of them. It's rather complicated to get back a handle to them to set their (tick-)labels.

Although it's a bit of a mystery what you intend to do, judging from the plot you only need two axes,

fig, ax = plt.subplots()
ax2 = ax.twinx()

you'd then plot the stacked plots to ax and the line plot to ax2.

Setting the ylabel of any of the two axes is done the usual way

ax2.set_ylabel("titleYlabel2", fontsize=14)

(same for ax). Removing the ticklabels can be done with

ax2.set_yticklabels([]) 

or

ax2.tick_params(labelright='off')

Alternatively removing the complete ticks works like

ax2.set_yticks([])

Upvotes: 2

Related Questions