Matplotlib:确保图例中出现完整的破折号图案

arg*_*dos 7 matplotlib

考虑这个简单的例子:

#setup figure
import matplotlib.pyplot as plt
import pylab
import math
import numpy as np
#output control
fig_width_pt = 345.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27               # Convert pt to inch
golden_mean = (math.sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt  # width in inches
fig_height = fig_width*golden_mean      # height in inches
fig_size =  [fig_width,fig_height]
params = {'backend': 'ps',
      'axes.labelsize': 10,
      'text.fontsize': 10,
      'legend.fontsize': 8,
      'xtick.labelsize': 8,
      'ytick.labelsize': 8,
      'text.usetex': True,
      'figure.figsize': fig_size,
      'figure.dpi': 1000,
      'savefig.dpi': 1000}
pylab.rcParams.update(params)

plt.figure().add_subplot(1,1,1)   
#plot some lines
dashes = [(None, None), [2, 2], [7, 4, 3, 4]]
titles = ["really long title one", "long title the second", "an even longer title, am I right?"]

for i in range(3):
    x = np.arange(i, i + np.pi/2.0, 0.01)
    y = np.sin(x) + i
    line,= plt.plot(x, y, label = titles[i])
    line.set_dashes(dashes[i])

plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

用'legend.fontsize'运行这个简单的程序:8产生 这个.请注意,点划线的虚线样式(图例中的第三个)很难解释.如果你放大,你会看到微小的黑点表明它对应于图形,但这并不理想.

我已经能够通过将图例fontsize更改为10来解决这个问题 这个,但这对我的实际数字来说太大了.

关于如何确保显示完整的linestyle/dashstyle的任何想法?

Aje*_*ean 12

您可以通过设置handlelength关键字来更改图例中的线条长度(不更改字体).尝试

plt.legend(handlelength=3)
Run Code Online (Sandbox Code Playgroud)