pandas plot value counts barplot in descending manner [duplicate]
You can do it by changing your plotting line like this df.letters.value_counts().sort_values().plot(kind = ‘barh’)
You can do it by changing your plotting line like this df.letters.value_counts().sort_values().plot(kind = ‘barh’)
Use pandas.DataFrame.pivot (no aggregation of values=) or pandas.DataFrame.pivot_table (with aggregation of values=) to reshape the dataframe from a long to wide form. The index will be on the y-axis, and the columns will be on the x-axis. See Reshaping and pivot tables for an overview. In [96]: result Out[96]: MutProb 0.001 0.005 0.010 0.050 0.100 … Read more
Well, there’s definitely more than one way to accomplish this. In this case, with only three colors needed, I would pick the colors myself by creating a LinearSegmentedColormap instead of generating them with cubehelix_palette. If there were enough colors to warrant using cubehelix_palette, I would define the segments on colormap using the boundaries option of … Read more
In the latest seaborn, you can use the countplot function: seaborn.countplot(x=’reputation’, data=df) To do it with barplot you’d need something like this: seaborn.barplot(x=df.reputation.value_counts().index, y=df.reputation.value_counts()) You can’t pass ‘reputation’ as a column name to x while also passing the counts in y. Passing ‘reputation’ for x will use the values of df.reputation (all of them, not … Read more
A. using a list of colors The easiest solution to make sure to have the same colors for the same categories in both plots would be to manually specify the colors at plot creation. # First bar plot ax = sns.barplot(data=a, x=’Scenario’, y=’Duration’, hue=”Program”, ci=None, palette=[“C0”, “C1”, “k”]) # … # Second bar plot ax2 … Read more
With newer versions of seaborn you can do following: import numpy as np import pandas as pd import seaborn as sns sns.set(color_codes=True) df = sns.load_dataset(‘titanic’) df.head() x,y = ‘class’, ‘survived’ (df .groupby(x)[y] .value_counts(normalize=True) .mul(100) .rename(‘percent’) .reset_index() .pipe((sns.catplot,’data’), x=x,y=’percent’,hue=y,kind=’bar’)) output Update: Also show percentages on top of barplots If you also want percentages, you can do … Read more
Extending the answer by @user2467675, here’s how I did it: def rand_jitter(arr): stdev = .01 * (max(arr) – min(arr)) return arr + np.random.randn(len(arr)) * stdev def jitter(x, y, s=20, c=”b”, marker=”o”, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs): return scatter(rand_jitter(x), rand_jitter(y), s=s, c=c, marker=marker, cmap=cmap, norm=norm, vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths, **kwargs) The stdev … Read more
You have to start a new figure in order to do that. There are multiple ways to do that, assuming you have matplotlib. Also get rid of get_figure() and you can use plt.savefig() from there. Method 1 Use plt.clf() import seaborn as sns import matplotlib.pyplot as plt iris = sns.load_dataset(‘iris’) length_plot = sns.barplot(x=’sepal_length’, y=’species’, data=iris) … Read more
The command plt.subplots(figsize = (10, 10)), indicates that you want to divide canvas and create the subplots on it. For your current requirement, you could do something like – ax = sns.barplot(x=’Submission Date’, y=’Count Handled’, data=df_cd) ax2 = ax.twinx() ax2.plot(ax.get_xticks(), df_cd.Rating) Here, you are creating the barplot first and adding the line plot over it … Read more
plt.tripcolor colors a mesh of triangles similar to how plt.pcolormesh colors a rectangular mesh. Also similar to pcolormesh, care has to be taken that there is one row and one column of vertices less than there are triangles. Furthermore, the arrays need to be made 1D (np.ravel). All this renumbering to 1D can be a … Read more