簡體   English   中英

Matplotlib降低帶有兩個y軸的花盆的圖例圖例線的厚度嗎?

[英]Matplotlib Reduce the Thickness of Plot Legend Line for a Pot with Two y-axes?

我有一個Python3.6代碼,該代碼生成兩個圖,一個帶有三個軸,另一個帶有兩個軸。 具有三個軸的圖具有帶有細線的圖例。

不幸的是,第二個圖例的線條的粗細等於標簽的高度:

在此處輸入圖片說明

這是第二個數字的代碼:

一個圖上有兩個圖

如何減少圖例中線條的粗細?

看起來該代碼並未發布到該帖子中。

這里是:

#two plots on one figure

def two_scales(ax1, time, data1, data2, c1, c2):

    ax2 = ax1.twinx()

    ax1.plot(time, data1, 'r')
    ax1.set_xlabel("Distance ($\AA$)")
    ax1.set_ylabel('Atom Charge',color='r')

    ax2.plot(time, data2, 'b')
    ax2.set_ylabel('Orbital Energy',color='b')
    return ax1, ax2



t = data[:,0]
s1 = data[:,3]
s2 = data[:,5]

fig, ax = plt.subplots()
ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b')


def color_y_axis(ax, color):
    for t in ax.get_yticklabels():
        t.set_color(color)
    return None
color_y_axis(ax1, 'r')
color_y_axis(ax2, 'b')

plt.title('Molecular Transforms')

patch_red = mpatches.Patch(color='red',label='Atom Charge')
patch_blue = mpatches.Patch(color='blue',label='Orbital Energy')
plt.legend(handles = [patch_red,patch_blue])

plt.draw()
plt.show()

name_plt = name+'-fig2.png'
fig.savefig(name_plt,bbox_inches='tight')

聽起來您想為圖例添加一行,但實際上使用了補丁。 示例和詳細信息可以在這里找到。 您可以使用類似於以下示例的代碼行:

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

line_red = mlines.Line2D([], [], color='red',label='Atom Charge')
line_blue = mlines.Line2D([], [], color='blue',label='Orbital Energy')
plt.legend(handles = [line_red,line_blue])
plt.show()

在此處輸入圖片說明

為圖例創建標簽的通常方法是使用藝術家的label參數在圖例中顯示ax.plot(...., label="some label") 使用設置了此標簽的實際藝術家,請確保圖例顯示與該藝術家相對應的符號; 如果是線圖,自然會是一條線。

import matplotlib.pyplot as plt

time=[1,2,3,4]; data1=[0,5,4,3.3];data2=[100,150,89,70]

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

l1, = ax1.plot(time, data1, 'r', label='Atom Charge')
ax1.set_xlabel("Distance ($\AA$)")
ax1.set_ylabel('Atom Charge',color='r')

l2, = ax2.plot(time, data2, 'b', label='Orbital Energy')
ax2.set_ylabel('Orbital Energy',color='b')


ax1.legend(handles=[l1,l2])

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM