0

I want to plot two graphs into the same figure with two different y-axis. In addition to it, I would like to add a grid and then save the plot as pdf.

My problem is that - while the grid is drawn correctly - the main axis are not shown anymore. How can they be plotted as well? I am using matplotlib 1.5.1 and Python 2.7.11.

Here is the plot:

enter image description here

And here is the code I am using:

import matplotlib.pyplot as plt import numpy as np from matplotlib.backends.backend_pdf import PdfPages pp = PdfPages('myplot.pdf') x = np.linspace(0.1, 10, 100) y1 = np.exp(x) y2 = np.log(x) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(x, y1, '-ro', markersize=5, label='y1') ax1.set_ylim(-0.1, 1.1 * max(y1)) ax1.set_ylabel('y1', fontsize=20) ax1.legend(loc='upper left') ax1.grid(ls='dotted', c='k') ax1.patch.set_facecolor('white') ax2 = ax1.twinx() ax2.grid(False) ax2.plot(x, y2, 'b-', label='y2') ax2.set_ylim(-0.1, 1.1 * max(y2)) ax2.set_ylabel('y2', fontsize=20) ax2.legend(loc='upper right') plt.xlim([-0.5, 1.05 * max(x)]) ax1.set_xlabel('x', fontsize=20) pp.savefig(fig) pp.close() plt.close(fig) 
3
  • It works for me... (Python 3.4.4 mac OS) but throws a RuntimeWarning: divide by zero encountered in log Commented Apr 7, 2016 at 6:10
  • @ReblochonMasque: Are you using the exact same code? The plot you get looks different from mine? The RunTimeWarning appears because log(0) is calculated (I changed that). Commented Apr 7, 2016 at 6:24
  • Yes, it shows the axis and a frame around the canvas. I'll post a png in a second. Commented Apr 7, 2016 at 6:29

1 Answer 1

1

It works for me with the plot correctly showing the axis like on the png added here under.

Same output for the following:

  • Python 3.4.4, iPython, Jupyter notebook, matplotlib 1.5.1
  • Python 2.7.11, iPython, Jupyter notebook, matplotlib 1.5.?

enter image description here

The code used is this one - same as the one posted except for the plt.show() that I added to produce the png image.

import matplotlib.pyplot as plt import numpy as np from matplotlib.backends.backend_pdf import PdfPages pp = PdfPages('myplot.pdf') x = np.linspace(0, 10, 100) y1 = np.exp(x) y2 = np.log(x) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(x, y1, '-ro', markersize=5, label='y1') ax1.set_ylim(-0.1, 1.1 * max(y1)) ax1.set_ylabel('y1', fontsize=20) ax1.legend(loc='upper left') ax1.grid(ls='dotted', c='k') ax1.patch.set_facecolor('white') ax2 = ax1.twinx() ax2.grid(False) ax2.plot(x, y2, 'b-', label='y2') ax2.set_ylim(-0.1, 1.1 * max(y2)) ax2.set_ylabel('y2', fontsize=20) ax2.legend(loc='upper right') plt.xlim([-0.5, 1.05 * max(x)]) ax1.set_xlabel('x', fontsize=20) #plt.show() pp.savefig(fig) pp.close() plt.close(fig) 

If you are using an ipython kernel, there might be some residual settings from previous work interfering with the rendering? Have you tried with a fresh kernel?

Sign up to request clarification or add additional context in comments.

5 Comments

That would be the expected output. Which version of matplotlib are you using (I use 1.5.1)? Any chance you could try it on Python 2.7.?
Yes, matplotlib 1.5.1 on python 3.4.4 on mac os. I get the exact same result with python 2.7.11 on mac os (you are lucky I had both kernels running concurrently ;)). kernel ran from jupyter notebook in both cases.
Ok, running it from the command line, indeed results in the correct plot. Now I still have to figure out why it works for the above toy example but not for the actual plot (where it still fails)... Thanks for your help - I upvote it for now and accept it later...
Sorry I could not solve it, but it is difficult to do much when unable to reproduce the fault... Glad I could modestly help you though. :) Cheers.
Oh, you helped a lot already, thanks! Seems I found the issue: In my actual file I also have an import seaborn statement which causes the trouble. If I comment it, everything works as intended. I would not have thought about this without your "interfering" comment. That's why the above example worked when I run it from the commen line: there was no import seaborn. Before I was running it in the same ipython session where I was running the seaborn stuff, too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.