图例对matplotlib中的水平网格透明

我正在上以下课程:

import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker

class matplotLIV():

    def __init__(self, BaseFilename, temperatures, length=None, width=None, area=None, title = '', ylim=None):
        self.BaseFilename = BaseFilename
        self.temperatures = temperatures
        if length and width:
            self.length = length
            self.width = width
            self.area = length*width*1e-5
        else:
            self.area = area
        self.title = title
        self.ylim = ylim

        filenames = [("%s_%sK.txt" % (self.BaseFilename, str(temp)), temp) for temp in self.temperatures]
        self.rawData = [(np.loadtxt(fname), temp) for fname, temp in filenames]
        self.colors = colors = ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#e6ab02', '#a6761d', '#666666']

        self.maxValueRow = (0,0,0)

    def plot(self):

        self.fig = plt.figure()
        self.ax1 = self.fig.add_subplot(111)
        ax1 = self.ax1
        ax1.tick_params(bottom='off')
        ax1.xaxis.tick_top()
        self.ax2 = ax1.twinx()
        ax2 = self.ax2
        self.ax3 = ax2.twiny()
        ax3 = self.ax3
        ax3.xaxis.tick_bottom()

        ax1.set_xlabel("current / A")
        ax1.xaxis.set_label_position('top')
        ax1.set_ylabel("voltage / V")
        ax2.set_ylabel("light intensity / arb. u.")
        ax3.set_xlabel(r'current density / $\mathregular{Acm^{-2}}$')
        ax3.xaxis.set_label_position('bottom')

        for i, (datafile, label) in enumerate(self.rawData):
            self.checkMaxValues(datafile)
            ax1.plot( datafile[:,0], datafile[:,1], color=self.colors[i], label='%sK' % str(label))
            ax2.plot( datafile[:,0], datafile[:,2], color=self.colors[i], label='%sK' % str(label), linewidth=2)


        ax1.margins(x=0)

        ax1.grid(True, axis='y')
        ax3.grid(True)

        start, end = ax1.get_xlim()

        self.setAxesScale(ax1, ax2)
        if self.ylim:
            ax2.set_ylim(top=self.ylim)

        ax3.set_xlim(start/self.area, end/self.area)
        leg = ax2.legend(loc='upper left')

        self.fig.suptitle(self.title, y=0.98, weight='bold')
        self.fig.subplots_adjust(top=0.86)

        loc = plticker.MultipleLocator(base=20.0) # this locator puts ticks at regular intervals
        ax3.xaxis.set_major_locator(loc)

    def checkMaxValues(self, data):
        maxInd = data.argmax(axis=0)[2]
        if data[maxInd][2] > self.maxValueRow[2]:
            self.maxValueRow = data[maxInd]

    def setAxesScale(self, ax1, ax2):
        yrange = ax1.get_ylim()
        y1Fraction = self.maxValueRow[1]/yrange[1]
        y2Fraction = y1Fraction - 0.02
        ax2.set_ylim(top=self.maxValueRow[2]/y2Fraction)

    def show(self):
        plt.savefig(self.BaseFilename + '.pdf')        
        plt.show()

您可以使用以下示例代码运行该代码:

import matplotLIV as mpliv

######## configuration

BaseFilename = "testdata"
temperatures = (5,)

area = 1e-8

######## end of configuration

liv = mpliv.matplotLIV(BaseFilename, temperatures, area=area)
liv.plot()
liv.show()

在此文件上:http : //pastebin.com/GMAC3mUu

我遇到的问题是图例对网格是透明的。奇怪的是,您只能通过图例框看到垂直网格:

通过图例看到的网格

这是一个错误吗?如果没有,如何设置图例使其不透明?

Tmdavison

问题是垂直网格位于ax3上,图例位于ax2上,因此网格是在图例之后绘制的。

一种解决方法是粘贴在下面(只是您需要修改的部分)。您需要在ax3上绘制图例,并明确告诉它所需的行和标签。

    # make a list for the lines that you are plotting
    l1 = []
    l2 = []
    for i, (datafile, label) in enumerate(self.rawData):
        self.checkMaxValues(datafile)
        # Give your lines some names (l1,l2)
        l1+=ax1.plot( datafile[:,0], datafile[:,1], color=self.colors[i], label='%sK' % str(label))
        l2+=ax2.plot( datafile[:,0], datafile[:,2], color=self.colors[i], label='%sK' % str(label), linewidth=2)

    # Define which lines to put in the legend. If you want l1 too, then use lns = l1+l2
    lns = l2
    labs = [l.get_label() for l in lns]

    ax1.margins(x=0)

    ax1.grid(True, axis='y')
    ax3.grid(True)

    start, end = ax1.get_xlim()

    self.setAxesScale(ax1, ax2)
    if self.ylim:
        ax2.set_ylim(top=self.ylim)

    ax3.set_xlim(start/self.area, end/self.area)
    # Set the legend on ax3, not ax2
    leg = ax3.legend(lns,labs,loc='upper left')

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

matplotlib中的重叠图例

来自分类Dev

matplotlib中的图例对齐

来自分类Dev

matplotlib中的表图例

来自分类Dev

Matplotlib图例中的字幕

来自分类Dev

matplotlib中的图例

来自分类Dev

在 matplotlib 中处理图例?

来自分类Dev

Windows Phone中的透明网格?

来自分类Dev

matplotlib图例中缺少标签

来自分类Dev

在matplotlib中对齐图例行

来自分类Dev

在matplotlib中获取图例的宽度

来自分类Dev

matplotlib中的阴影图例行

来自分类Dev

散点图:在图例中显示缺少的因子水平

来自分类Dev

R中图例水平图间距

来自分类Dev

matplotlib中图例中的重复项?

来自分类Dev

matplotlib中图例中的多个标题

来自分类Dev

当水平方向的图例方向为水平时,是否可以防止图例上的图例向上移动到图表中?

来自分类Dev

在matplotlib中绘制网格图案

来自分类Dev

图例覆盖图例-Matplotlib

来自分类Dev

extjs网格中的水平缓冲

来自分类Dev

如何清除图例中的matplotlib标签?

来自分类Dev

python matplotlib中基于Colorbar的图例

来自分类Dev

通过matplotlib图例中的标记删除线

来自分类Dev

matplotlib散点图图例中缺少标签

来自分类Dev

如何确定matplotlib图例中的项目顺序?

来自分类Dev

在matplotlib中自定义图例

来自分类Dev

如何从Matplotlib规范中删除图例?

来自分类Dev

在Matplotlib中显示多个线图例

来自分类Dev

Matplotlib图例未显示在绘图方法中

来自分类Dev

在matplotlib的图例中插入先前计算的值