[Tutor] Problem with Plot Legend
Stephen P. Molnar
s.molnar at sbcglobal.net
Tue Jun 13 14:55:22 EDT 2017
I am using Python3.6 in the Spyder3IDE and have a problem with the
legend for a plot.
I have attached the pg file.
The code (in part) is:
import numpy as np
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
.
.
.
(the portion of the code for Figure q1 has no problems)
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]
# Create axes
fig, ax = plt.subplots()
ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b')
# Change color of each axis
def color_y_axis(ax, color):
"""Color your axes."""
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')
The problem is that the legend is where I want it in the figure and
contains what I want with one exception:
The colored lines are too thick, the same width as the text.
I've look in the literature and Googled for the solution, but I'm
beginning to think that I don't know just what the question that I
should be asking, hence no results.
A pointer towards thech solution to this problem will be much appreciated.
Thanks in advance.
--
Stephen P. Molnar, Ph.D.
Consultant
www.molecular-modeling.net
(614)312-7528 (c)
Skype: smolnar1
More information about the Tutor
mailing list