I`d like to have a multiline figure with a dataframe.
Original data is as following:
from numpy.random import randn
import numpy as np
df=pd.DataFrame()
df['Years']=range(1995,2013)
np.random.seed(0)
df['Goverment']=randn(len(df.Years))
df['Household']=randn(len(df.Years))
df['Corporate']=randn(len(df.Years))
print(df)
and I want to set the legend along fully the bound pf figure box. I referred to the answer of @Joe Kington but this problem hasn`t been solved.
For plotting:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))
ax = plt.subplot(111)
ax.plot(df.Years, df.Government,ls='--',label='Goverment',color='black')
ax.plot(df.Years,df.Household,ls=':',label='Household',color='black')
ax.plot(df.Years,df.Corporate,ls='-',label='Corporate',color='black')
plt.xlabel("common X")
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
box.width, box.height * 1])
# Put a legend below current axis
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1),borderaxespad=1,mode='expand',ncol=3)
plt.show()
and the following is my result. Apparently the mode='expand'
doesn`t work here.
My questions are: 1. Why the values on X axis are not integral but floats? 2. How to expand the legend box into one line instrad fully along the bound of box?