使用网格时主轴未显示 [英] Main axis are not shown when using grid

查看:105
本文介绍了使用网格时主轴未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个图形绘制到具有两个不同y轴的同一图形中.除了它,我想添加一个网格,然后将图另存为pdf.

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.

我的问题是-在正确绘制网格的同时-不再显示主轴.如何绘制它们呢?我正在使用matplotlib 1.5.1Python 2.7.11.

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.

这是情节:

这是我正在使用的代码:

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)

推荐答案

它对我有用,它的图正确显示了轴,就像在下面添加的png上一样.

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

以下内容的输出相同:

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

使用的代码是此代码-与我发布的代码相同,除了我添加来生成png图像的plt.show().

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)

如果您使用的是ipython内核,以前的工作中可能会有一些残留设置会干扰渲染?您是否尝试过使用新内核?

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?

这篇关于使用网格时主轴未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆