matplotlib: Aligning y-axis labels in stacked scatter plots

You can use the set_label_coords method. import matplotlib.pylab as plt import random import matplotlib.gridspec as gridspec random.seed(20) data1 = [random.random() for i in range(10)] data2 = [random.random()*1000 for i in range(10)] gs = gridspec.GridSpec(2,1) fig = plt.figure() ax = fig.add_subplot(gs[0]) ax.plot(data1) ax.set_ylabel(r’Label One’, size =16) ax.get_yaxis().set_label_coords(-0.1,0.5) ax = fig.add_subplot(gs[1]) ax.plot(data2) ax.set_ylabel(r’Label Two’, size =16) ax.get_yaxis().set_label_coords(-0.1,0.5)

decompose() for time series: ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None

Having the same ValueError, this is just the result of some testing and little research on my own, without the claim to be complete or professional about it. Please comment or answer whoever finds something wrong. Of course, your data should be in the right order of the index values, which you would assure with … Read more

Plot a histogram such that the total height equals 1

When plotting a normalized histogram, the area under the curve should sum to 1, not the height. In [44]: import matplotlib.pyplot as plt k=(3,3,3,3) x, bins, p=plt.hist(k, density=True) # used to be normed=True in older versions from numpy import * plt.xticks( arange(10) ) # 10 ticks on x axis plt.show() In [45]: print bins [ … Read more

Dollar Sign with Thousands Comma Tick Labels

You can use StrMethodFormatter, which uses the str.format() specification mini-language. import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as mtick df = pd.DataFrame({‘A’: [‘A’, ‘B’], ‘B’: [1000,2000]}) fig, ax = plt.subplots(1, 1, figsize=(2, 2)) df.plot(kind=’bar’, x=’A’, y=’B’, align=’center’, width=.5, edgecolor=”none”, color=”grey”, ax=ax) fmt=”${x:,.0f}” tick = mtick.StrMethodFormatter(fmt) ax.yaxis.set_major_formatter(tick) plt.xticks(rotation=25) plt.show()

Simple and two head arrows

You can create a double-headed arrow using the annotate method with blank text annotation and setting the arrowprops dict to include arrowstyle=”<->” as shown below: import matplotlib.pyplot as plt plt.annotate(s=””, xy=(1,1), xytext=(0,0), arrowprops=dict(arrowstyle=”<->”)) plt.show()