Friday 15 June 2012

python - Pyplot: using percentage on x axis -



python - Pyplot: using percentage on x axis -

i have line chart based on simple list of numbers. default x-axis increment of 1 each value plotted. percentage instead can't figure out how. instead of having x-axis 0 5, go 0% 100% (but keeping reasonably spaced tick marks. code below. thanks!

from matplotlib import pyplot plt mpl_toolkits.axes_grid.axislines import subplot data=[8,12,15,17,18,18.5] fig=plt.figure(1,(7,4)) ax=subplot(fig,111) fig.add_subplot(ax) plt.plot(data)

the code below give simplified x-axis percentage based, assumes each of values spaces as between 0% , 100%.

it creates perc array holds evenly-spaced percentages can used plot with. adjusts formatting x-axis includes percentage sign using matplotlib.ticker.formatstrformatter. unfortunately uses old-style string formatting, opposed new style, old style docs can found here.

import matplotlib.pyplot plt import numpy np import matplotlib.ticker mtick info = [8,12,15,17,18,18.5] perc = np.linspace(0,100,len(data)) fig = plt.figure(1, (7,4)) ax = fig.add_subplot(1,1,1) ax.plot(perc, data) fmt = '%.0f%%' # format want ticks, e.g. '40%' xticks = mtick.formatstrformatter(fmt) ax.xaxis.set_major_formatter(xticks) plt.show()

python matplotlib

No comments:

Post a Comment