python 2.7 - Matplotlib scatter plot different colors in legend and plot -
i have scatter plot of multiple y-values same x-value, in matplotlib (python 2.7). there different colors plotted y-values. see plot below.
now, here code:
import numpy np import pandas pd import matplotlib.pyplot plt import pylab pl import matplotlib.cm cm # generate test x, y , y_error values: df2a = pd.dataframe(np.random.rand(10,5), columns = list('abcde')) df2a.insert(0,'x_var_plot',range(1,df2a.shape[0]+1)) df2a_err = pd.dataframe(np.random.rand(df2a.shape[0],df2a.shape[1]-1), columns = df2a.columns.tolist()[1:]) df2a_err.insert(0,'x_var_plot',range(1,df2a.shape[0]+1)) fig = plt.figure(1) ax = fig.add_subplot(111) ![errorbars problem][1] colors = iter(cm.rainbow(np.linspace(0, 1, df2a.shape[1]))) # generate plot: col in df2a.columns.values.tolist()[1:]: ax.errorbar(df2a['x_var_plot'], df2a[col], yerr=df2a_err[col], fmt='o') ax.scatter(df2a['x_var_plot'], df2a[col], color=next(colors), label=col) ax.legend(loc='best', scatterpoints = 1) plt.show()
the problem colors in plot symbols different symbol colors in legend. problem occurs when add errorbars. wrong between connection between legend symbol color , scatter plot error bars.
is there way fix colors in legend same colors of scatter plot points, when include error bars?
in plot there different colors error bars , scatter plot points. can make them same this:
c = next(colors) ax.errorbar(df2a['x_var_plot'], df2a[col], color = c, yerr=df2a_err[col], fmt='o') ax.scatter(df2a['x_var_plot'], df2a[col], color = c, label=col)
Comments
Post a Comment