Pyplot sorting y-values automatically

From matplotlib 2.1 on you can plot categorical variables. This allows to plot plt.bar([“apple”,”cherry”,”banana”], [1,2,3]). However in matplotlib 2.1 the output will be sorted by category, hence alphabetically. This was considered as bug and is changed in matplotlib 2.2 (see this PR). In matplotlib 2.2 the bar plot would hence preserve the order. In matplotlib … Read more

Matplotlib: setting x-limits also forces tick labels?

The additional ticklabels that overlap originate from some minor ticklabels, which are present in the plot. To get rid of them, one can set the minor formatter to the NullFormatter: plt.gca().xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter()) The complete code from the question might then look like import matplotlib.pyplot as plt import matplotlib.ticker import numpy as np x = np.linspace(0,2.5) y … Read more

Get default line colour cycle

In matplotlib versions >= 1.5, you can print the rcParam called axes.prop_cycle: print(plt.rcParams[‘axes.prop_cycle’].by_key()[‘color’]) # [u’#1f77b4′, u’#ff7f0e’, u’#2ca02c’, u’#d62728′, u’#9467bd’, u’#8c564b’, u’#e377c2′, u’#7f7f7f’, u’#bcbd22′, u’#17becf’] Or equivalently, in python2: print plt.rcParams[‘axes.prop_cycle’].by_key()[‘color’] In versions < 1.5, this was called color_cycle: print plt.rcParams[‘axes.color_cycle’] # [u’b’, u’g’, u’r’, u’c’, u’m’, u’y’, u’k’] Note that the default color cycle changed … Read more

Annotate data points while plotting from Pandas DataFrame

Here’s a (very) slightly slicker version of Dan Allan’s answer: import matplotlib.pyplot as plt import pandas as pd import numpy as np import string df = pd.DataFrame({‘x’:np.random.rand(10), ‘y’:np.random.rand(10)}, index=list(string.ascii_lowercase[:10])) Which gives: x y a 0.541974 0.042185 b 0.036188 0.775425 c 0.950099 0.888305 d 0.739367 0.638368 e 0.739910 0.596037 f 0.974529 0.111819 g 0.640637 0.161805 h … Read more

Embedding small plots inside subplots in matplotlib

I wrote a function very similar to plt.axes. You could use it for plotting yours sub-subplots. There is an example… import matplotlib.pyplot as plt import numpy as np #def add_subplot_axes(ax,rect,facecolor=”w”): # matplotlib 2.0+ def add_subplot_axes(ax,rect,axisbg=’w’): fig = plt.gcf() box = ax.get_position() width = box.width height = box.height inax_position = ax.transAxes.transform(rect[0:2]) transFigure = fig.transFigure.inverted() infig_position = … Read more

matplotlib strings as labels on x axis

Use the xticks command. import matplotlib.pyplot as plt t11 = [’00’, ’01’, ’02’, ’03’, ’04’, ’05’, ’10’, ’11’, ’12’, ’13’, ’14’, ’15’, ’20’, ’21’, ’22’, ’23’, ’24’, ’25’, ’30’, ’31’, ’32’, ’33’, ’34’, ’35’, ’40’, ’41’, ’42’, ’43’, ’44’, ’45’, ’50’, ’51’, ’52’, ’53’, ’54’, ’55’] t12 = [173, 135, 141, 148, 140, 149, 152, … Read more