0

I am new to matplotlib. I've been able to put together the following plot:

enter image description here

I did the axis tick labels on the left using:

plot.yticks([n + 0.5 for n in range(len(endIDs))], endIDs)

I found that I had to add to the 0.5, to get it to center better. But only when the entries got to be a lot, I'm not sure why. But what I want is to have a corresponding axis on the right side that prints some info. I chose to just have a loop that looked like:

plot.text(1.0, (index + 0.5) / len(endIDs), eachText, horizontalalignment='left', verticalalignment='center', transform=ax.transAxes)

But they don't quite line up. What is the best technique to use to get the labels on the right to line up with the tick marks on the left? I don't actually need a second axes, I just want the same axis/ticks on the right, but with different labels.

(I'm using Python3 if that matters)

UPDATE

Rather than use pyplot.yticks and a bunch of text placements, I've now moved to the following:

fig, axLeft = plot.subplots()
axLeft.set_yticks(range(len(endIDs)))
axLeft.set_yticklabels(endIDs)
axRight = axLeft.twinx()
axRight.set_yticks(range(len(endIDs)))
axRight.set_yticklabels(missRates)

Unfortunately, it still produces output similar to the above where the left side seems to have inset on the ends and lines up with the actual plot series, but the right side is stretched from corner to corner with no inset. Which is weird because I gave both axes the exactly same set_yticks argument.

Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
  • I would use a twin axes object with custom y-axis formatters and locators. Here's an answer the shows you how to setup the figure: https://stackoverflow.com/questions/14762181/adding-a-y-axis-label-to-secondary-y-axis-in-matplotlib/14762601#14762601 Then you'll need to dig around the mpl docs and SO to learn how to use custom locators and formatters. – Paul H Oct 31 '17 at 16:14
  • Are the number of ticks the same? If not, one thing to try is to pad the shorter list of ticks. – Acccumulation Oct 31 '17 at 16:15
  • Yes, I want the ticks at the same location. I'm trying `twinx` right now... it still doesn't line them up. I'll update above... – Travis Griggs Oct 31 '17 at 16:16
  • 1
    set_ybound/get_ybound between the two axes seems to be the trick. Anyone want to post that for the answer? :D – Travis Griggs Oct 31 '17 at 16:28

0 Answers0