简体   繁体   English

matplotlib中寄生轴的网格线

[英]Grid lines in parasitic axes in matplotlib

Can you draw the grid lines in a plot with parasitic axes in matplotlib? 你能在matplotlib的寄生轴图中绘制网格线吗?

I try this, based on the samples for grids and for parasitic axes, but grid drawing is not performed: 我尝试这个,基于网格和寄生轴的样本,但不执行网格绘制:

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
import matplotlib.pyplot as plt

fig = plt.figure(1)

host = SubplotHost(fig, 111)
fig.add_subplot(host)

par = host.twinx()

host.set_xlabel("Distance")
host.set_ylabel("Density")
par.set_ylabel("Temperature")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par.plot([0, 1, 2], [0, 3, 2], label="Temperature")

host.axis["left"].label.set_color(p1.get_color())
par.axis["right"].label.set_color(p2.get_color())

host.grid(True)

host.legend()

plt.show()

From the discussion here it looks like this is a bug in the .99 release. 这里的讨论来看,这似乎是.99版本中的一个错误。

(I'm not sure why it works for doug but no combination of rcParams works for me on version 0.99.1.1-r1.) (我不确定为什么它适用于道格,但在版本0.99.1.1-r1上没有rcParams的组合对我有用。)

From that link the answer is to make a call to: 从该链接的答案是打电话给:

host.toggle_axisline(False)

What the toggle_axisline does is simply to make the xaxis and yaxis (which are responsible for drawing ticks, ticklabels, etc in the mainline mpl) visible again, and make axis["bottom"] and etc invisible. toggle_axisline的作用只是让xaxis和yaxis(它们负责在主线mpl中绘制刻度线,刻度标签等)再次可见,并使轴[“bottom”]等不可见。

The whole program becomes: 整个计划变为:

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
import matplotlib.pyplot as plt

fig = plt.figure(1)

host = SubplotHost(fig, 111)
fig.add_subplot(host)

par = host.twinx()

host.set_xlabel("Distance")
host.set_ylabel("Density")
par.set_ylabel("Temperature")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par.plot([0, 1, 2], [0, 3, 2], label="Temperature")

host.axis["left"].label.set_color(p1.get_color())
par.axis["right"].label.set_color(p2.get_color())

host.toggle_axisline(False)
host.grid(True)

host.legend()

plt.show()

在此输入图像描述

For plots of this type, it's not always easy to tell at a glance which object should make the call to 'grid'. 对于这种类型的情节,一眼就能看出哪个对象应该调用“网格”并不总是很容易。

One way around this inconvenience--ie, to get the grid lines on your plot without having to change any of your code and worry about whether you have the right object calling 'grid'--is to edit your config file . 绕过这种不便的一种方法 - 即在你的绘图上获得网格线而不必更改任何代码并担心你是否有正确的对象调用'grid' - 就是编辑你的配置 文件 That might be enough for you to do exactly what you need to do, but just in case: 这可能足以让你做到你需要做的事情,但以防万一:

  1. download the sample matplotlibrc file here or retrieve your copy at site-packages/matplotlib/mpl-data/; 这里下载示例matplotlibrc文件或在site-packages / matplotlib / mpl-data /中检索您的副本;

  2. at about line 195 or so of this file, look for a heading (as a comment) "### AXES"; 在该文件的约195行左右,查找标题(作为注释)“### AXES”;

  3. five-six lines below that heading you'll see ' axes-grid '--uncomment this line and set the value to 'True'; 在该行标题下方的五六行您将看到' axes-grid ' - 取消该行,并将该值设置为'True';

  4. now read down this file to about lines 235 or so where you will see the header '### GRIDS'; 现在读下这个文件大约235行左右,你会看到标题'### GRIDS';

  5. uncomment the next three lines ('grid.color', 'grid.linestyle', and grid.linewidth') and supply reasonable values for those three parameters (mine are: 'darkslategray', ':', and 0.7, respectively). 取消注释接下来的三行('grid.color','grid.linestyle'和grid.linewidth')并为这三个参数提供合理的值(我的分别为:'darkslategray',':'和0.7)。 (The ':' value means my grid lines will be dotted lines.) (':'值表示我的网格线将是虚线。)

  6. save that file as: ~/.matplotlibrc/matplotlibrc (in other words, create a directory in your top level user directory called '.matplotlibrc', don't forget the leading '.', then name this file you've been editing 'matplotlibrc'. 将该文件保存为:〜/ .matplotlibrc / matplotlibrc(换句话说,在顶级用户目录中创建一个名为'.matplotlibrc'的目录,不要忘记前导'。',然后将此文件命名为您一直在编辑的文件'matplotlibrc'。

That's it. 而已。 (The downside is when you are creating plots for which you do not want grid lines--for those, i would keep this config file as is and create additional config files as needed and switch among them (see the relevant Matplotlib page for how to do that.) (缺点是当你创建你不想要网格线的图时 - 对于那些,我会保持这个配置文件不变并根据需要创建其他配置文件并在它们之间切换(参见相关的Matplotlib页面 ,了解如何去做。)

Also, this config file is easy to edit using ipython--that's probably how most users do it, but that might have been confusing here. 此外,这个配置文件很容易使用ipython编辑 - 这可能是大多数用户这样做的原因,但这可能会令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM