I have several subplots that I've created using matplotlib. Once I plot the data, I need to go back and draw lines between data points in a for loop. My data file is large and this takes python a very long time...
Is there a way to speed this up? Here is my code:
def generateHistogram(x, y, ax):
x = np.log10(x)
xerror = []
numData = len(x)
plt.rcParams['lines.solid_capstyle'] = 'butt'
for dataIndex in range(0, numData-1):
xerror.append(np.divide(np.subtract(x[dataIndex+1], x[dataIndex]), 2.0))
for errorIndex in range(0, len(x)):
if (errorIndex == 0):
ax.semilogx((np.power(10, (x[errorIndex]-xerror[errorIndex])), np.power(10, x[errorIndex])),
(y[errorIndex], y[errorIndex]), linewidth=2, color='k')
if (errorIndex == len(xerror)):
ax.semilogx((np.power(10, x[errorIndex]), np.power(10, (x[errorIndex]+xerror[errorIndex-1]))),
(y[errorIndex], y[errorIndex]), linewidth=2, color='k')
if (errorIndex < len(xerror)):
ax.semilogx((np.power(10, x[errorIndex]), np.power(10, (x[errorIndex]+xerror[errorIndex]))),
(y[errorIndex], y[errorIndex]), linewidth=2, color='k')
ax.semilogx((np.power(10, (x[errorIndex+1]-xerror[errorIndex])), np.power(10, x[errorIndex+1])),
(y[errorIndex+1], y[errorIndex+1]), linewidth=2, color='k')
verticleLineXPos = np.power(10, (x[errorIndex]+xerror[errorIndex]))
ax.semilogx((verticleLineXPos, verticleLineXPos), (y[errorIndex], y[errorIndex+1]),
linewidth=2, color='k')
return xerror;
This basically draws line on each of the subplots (where the x-axis is in a semilogx scale) at the positions I need. Do you have any suggestions for improving the performance?
xerror = np.diff (x) / 2. Can you post a picture of what you are trying to do? You might also want to look into aLineCollectionartist.ax.vlinesmatplotlib.org/api/axes_api.html#matplotlib.axes.Axes.vlines