I hope that my explanation is clear enough. I would like to make a scatter plot showing its time series by its color evolution (e.g., light red to dar red), where I have three independent time series data. What I want to do is changing their color from 150 to 185 with color evolutions from light red to dark red, light blue to dark blue, light green to dark green. The current code of drawing each time series is: trj_up = open('./up_a_2.dat','r')Įach file contains the following time series (see below). It is also possible to use this approach for more than one column to color by, but the legend is getting a mess.I would like to make a scatter plot showing its time series by its color evolution (e.g., light red to dar red), where I have three independent time series data. In my case I have timeseries data, so the MultiIndex consists of datetime and categories. This procedure may make more sense for other data series. With this method you do not have to manually specify the colors. Price = Ĭolor =ĭf = pd.DataFrame(dict(carat=carat, price=price, color=color))ĭf.set_index().unstack('color').plot(style='o') This takes the index as the x value, the value as the y value and plots each column separately with a different color.Ī DataFrame in this form can be achieved by using set_index and unstack. You can obtain the legend handles and change their colors individually: ax plt.gca () leg ax.getlegend () leg.legendHandles 0.setcolor ('red') leg.legendHandles 1.setcolor ('yellow') Adding to the other answers I've had trouble in the past changing color of legend markers with setcolor. Normally when quickly plotting a DataFrame, I use pd.ot(). In the first plot, the default colors are chosen by passing min-max scaled values from the array of category level ints pd.factorize(iris) to the call method of the plt.cm.viridis colormap object. ![]() I chose the "tab10" discrete (aka qualitative) colormap here, which does a better job at signaling the color factor is a nominal categorical variable. Plt.legend(handles=handles, title='Color') Levels, categories = pd.factorize(df)Ĭolors = # using the "tab10" colormap To choose your own colormap and add a legend, the simplest approach is this: import matplotlib.patches In this case "viridis" is not a good default choice because the colors appear to imply a sequential order rather than purely nominal categories. This creates a plot without a legend, using the default "viridis" colormap. Plt.gca().set(xlabel='Carat', ylabel='Price', title='Carat vs. The easiest way is to simply pass an array of integer category levels to the plt.scatter() color parameter. To select a color, I've created a colors dictionary, which can map the diamond color (for instance D) to a real color (for instance tab:blue). ![]() It then iterates over these groups, plotting for each one. This code assumes the same DataFrame as above, and then groups it based on color. ot(ax=ax, kind='scatter', x='carat', y='price', label=key, color=colors) ![]() If you don't want to use seaborn, use oupby to get the colors alone, and then plot them using just matplotlib, but you'll have to manually assign colors as you go, I've added an example below: fig, ax = plt.subplots(figsize=(6, 6)) sns.lmplot(x='carat', y='price', data=df, hue='color', fit_reg=False) ![]() Selecting hue='color' tells seaborn to split and plot the data based on the unique values in the 'color' column.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |