Python Matplotlib散点图的自定义图例格式
我使用以下代码从多个数据帧中绘制多个数据集,使用Python Matplotlib散点图的自定义图例格式,python,matplotlib,Python,Matplotlib,我使用以下代码从多个数据帧中绘制多个数据集,使用matplotlib.pyplot: 导入matplotlib.pyplot作为plt def打印增量(dfs、标签): 标签=iter(标签) 标记=iter(['o','s','D']) 颜色=国际热核实验堆([‘蓝色’、‘红色’、‘绿色’])) 图,ax=plt.子批次() ax.set_ylabel(r“$\Delta\Delta{iso}^{C-M}$(ppm)”,fontsize=14) 对于dfs中的df: 标记=下一个(标记) 颜色
matplotlib.pyplot
:
导入matplotlib.pyplot作为plt
def打印增量(dfs、标签):
标签=iter(标签)
标记=iter(['o','s','D'])
颜色=国际热核实验堆([‘蓝色’、‘红色’、‘绿色’]))
图,ax=plt.子批次()
ax.set_ylabel(r“$\Delta\Delta{iso}^{C-M}$(ppm)”,fontsize=14)
对于dfs中的df:
标记=下一个(标记)
颜色=下一个(颜色)
标签=下一个(标签)
第1行,=ax.plot(df['label'],df['delta_c-m_a'],color=color,linewidth=0,fillstyle='full',marker=marker,markeredgewidth=2.0,label=label+“a”)
line2,=ax.plot(df['label'],df['delta_c-m_b'],color=color,linewidth=0,fillstyle='none',marker=marker,markeredgewidth=2.0,label=label+“b”)
平面网格(轴='x')
plt.图例(fontsize=12)
返回无花果,斧头
调用plot_delta([df1,df2,df3],'Form I','Form II','Form III'])
返回绘图:
我想创造一些看起来更像这样的东西,而不是这个庞大的传说:
如何简单地自定义图例的格式?我已经搜索了Matplotlib的文档,但没有找到类似的内容。可以使用(Matplotlib.axes.axes.text)而不是图例方法。
查看网页底部链接的许多示例。这里有一个很好的例子。我已经根据这个答案修改了你的代码。但是,不能在处理程序之间插入斜杠。标签是一个列表,在从参数获取的字符串中添加了“a/B”
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerTuple
def plot_deltas(dfs, labels):
labels = iter(labels)
markers = iter(['o', 's', 'D'])
colors = iter(['blue', 'red', 'green'])
fig, ax = plt.subplots()
ax.set_ylabel(r"$\Delta\delta_{iso}^{C-M}$ (ppm)", fontsize=14)
new_handles = []
new_labels = []
for df in dfs:
marker = next(markers)
color = next(colors)
label = next(labels)
line1, = ax.plot(df['label'], df['delta_c-m_a'], color=color, linewidth=0, fillstyle='full', marker=marker, markeredgewidth=2.0, label=label+" A")
line2, = ax.plot(df['label'], df['delta_c-m_b'], color=color, linewidth=0, fillstyle='none', marker=marker, markeredgewidth=2.0, label=label+" B")
n_h = (line1, line2)
new_handles.append(n_h)
n_l = '{}{}'.format(label, ' A / B')
new_labels.append(n_l)
plt.grid(axis='x')
plt.legend(new_handles, new_labels, handler_map={tuple: HandlerTuple(ndivide=2)},handlelength=3,fontsize=12)
return fig, ax
plot_deltas([df1, df2, df3], ['Form I', 'Form II', 'Form III'])
plt.show()