The following code is used to produce a barchart. I would like to rotate it so that it becomes vertical e.g. the current labels at the x axis go to the y axis ,the current y axis labels to the x axis and the bars rotate accordingly.
I am new to matplotlib and python so any help would be welcomed.
def plot_coefficients(classifier, feature_names, top_features=40):
if classifier.__class__.__name__ == 'SVC':
coef = classifier.coef_
coef2 = coef.toarray().ravel()
coef1 = coef2[:len(feature_names)]
else:
coef2 = classifier.coef_.ravel()
coef1 = coef2[:len(feature_names)]
top_positive_coefficients = np.argsort(coef1)[-top_features:]
top_negative_coefficients = np.argsort(coef1)[:top_features]
top_coefficients = np.hstack([top_negative_coefficients, top_positive_coefficients])
# create plot
plt.figure(figsize=(15, 5))
colors = ['red' if c < 0 else 'blue' for c in coef1[top_coefficients]]
plt.bar(np.arange(2 * top_features), coef1[top_coefficients], color=colors)
feature_names = np.array(feature_names)
plt.xticks(np.arange(1, 1 + 2 * top_features), feature_names[top_coefficients], rotation=90, ha='right')
plt.show()
Update
Expected output:
Look at the matplotlib method barh. You can find example from: https://matplotlib.org/gallery/lines_bars_and_markers/barh.html
Related
I'm trying to plot a graph using Bokeh.
I did it using matplotlib but I can't figure out how I can plot 2D array using Bokeh.
This is my code for plotting with matplotlib:
[row, col] = point_data.shape
# select columns names
text = point_data.columns
# find string index:
range_ind = np.array(text.str.find('Range_cm')) > -1
range_cm_mat = point_data.iloc[0:row, range_ind].values
plt.figure(figsize=(19, 10))
plt.plot(range_cm_mat, '.', markersize=0.5, color='#1f77b4')
plt.show()
My data I'm plotting is from a CSV file and it is filtered by the string above.
Please help me figure out how I can show this plot with Bokeh library
This is what I tried and it isn't the way...
def make_scatter(title, x_title, y_title, x, y):
p = figure(title=title, toolbar_location="below", background_fill_color="#fafafa")
p.circle(x, y) # , alpha=0.5)
p.y_range.start = 0
p.legend.location = "center_right"
p.legend.background_fill_color = "#fefefe"
p.xaxis.axis_label = x_title
p.yaxis.axis_label = y_title
p.grid.grid_line_color = "white"
return p
x = np.arange(1, np.size(plot[0]), 1)
for i in range (np.size(plot[0])):
p1 = make_scatter("Range/Frames", 'Range [m]', 'Frames', [pt[i] for pt in plot], x)
show(p1)
This is how the data looks like:
2D array:
I want to use matpoltlib to make a plot that with a constant y axis(always from 0 to 14 and the gap is 1), since I want to make labels for them and my dot values will be(x, y) where y is from 0 to 14 gap 1, and a changing x axis. I already tried to play with y ticks. And here is my code for that:
fig, ax = plt.subplots()
fig.canvas.draw()
plt.yticks(np.arange(0, 14, 1))
labels = [item.get_text() for item in ax.get_yticklabels()]
labels[1] = 'Not Detected'
labels[2] = 'A/G'
labels[3] = 'G/G'
labels[4] = 'C/T'
labels[5] = 'C/C'
labels[6] = 'A/A'
labels[7] = '-1'
labels[8] = 'ε3/ε3'
labels[9] = 'A/C'
labels[10] = 'T/T'
labels[11] = 'C/G'
labels[12] = 'ε2/ε3'
labels[13] = 'G/T'
ax.set_yticklabels(labels)
what I'm thinking about is to use some values or lines with white color so those y axis will appear. But I'm looking for a more efficient way of doing it. And here is the diagram I generated with the current code. It only shows C/C right now and I want all labels to appear in the diagram.
I tried draw white points with:
x1 = np.arange(n)
y1 = np.arange(1,15,1)
plt.scatter(x1,y1,color = 'white')
Which did give me what I want: But I was wondering whether there is a lib setting that can do this.
I would recommend just using a fixed locator and fixed formatter for your y axis. The function, ax.set_yticklabels() is simply a convenience wrapper for these tick methods.
I would also recommend having your y_labels in a list or using a loop structure as this is a more generalizable and modifiable implementation.
If I'm understanding the goals of your plot correctly, something like this may work well for you.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
#make some data
x = np.arange(25)
y = np.random.randint(1, 14, size=25)
#convert y labels to a list
y_labels = [
'Not Detected','A/G','G/G','C/T','C/C','A/A',
'-1','ε3/ε3', 'A/C','T/T','C/G','ε2/ε3','G/T'
]
#define figure/ax and set figsize
fig, ax = plt.subplots(figsize=(12,8))
#plot data, s is marker size, it's points squared
ax.scatter(x, y, marker='x', s=10**2, color='#5d2287', linewidth=2)
#set major locator and formatter to fixed, add grid, hide top/right spines
locator = ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(np.arange(1, 14)))
formatter = ax.yaxis.set_major_formatter(mpl.ticker.FixedFormatter(y_labels))
grid = ax.grid(axis='y', dashes=(8,3), alpha=0.3, color='gray')
spines = [ax.spines[x].set_visible(False) for x in ['top','right']]
params = ax.tick_params(labelsize=12) #increase label font size
I am new to python and trying to create a plot with one y variable and two x variables. I want the two lines to show up in the same plot with different labels and colors/makrers. Here is my code to attempt:
y = lambda x: x**(-3)
z = lambda x: x**(-10)
x_grid = np.linspace(1,10, 10)
v_y = []
v_z = []
for i in x_grid:
vy=y(i)
v_y.append(vy)
v_y_array = np.array(v_y)
for j in x_grid:
vz=y(j)
v_z.append(vz)
v_z_array = np.array(v_z)
fig, ax = plt.subplots()
line1, = ax.plot(x_grid, v_y_array, 'b--', label='function 1')
line2, = ax.plot(x_grid, v_z_array, 'r--', label='function 1')
ax.legend()
plt.show()
However, the figure only shows the second line and ignores the first.
But if I try to do the following, it works out fine.
y = lambda x: x**(-3)
z = lambda x: x**(-10)
x_grid = np.linspace(1,10, 10)
v_y = []
v_z = []
for i in x_grid:
vy=y(i)
v_y.append(vy)
v_y_array = np.array(v_y)
for j in x_grid:
vz=y(j)
v_z.append(vz)
v_z_array = np.array(v_z)
fig,ax=plt.subplots()
ax.plot(x_grid,v_y_array,'r--', v_z_array, 'b--', label='x**(-3) function')
ax.set_title('Two Functions')
ax.legend(['x**(-3) function','x**(-10) function'])
plt.show()
I wonder what was the problem with my first set of codes that won't produce the figure that I want?
The reason why the red and blue lines don't overlap in the second plot lies in the official documentation.
ax.plot(x_grid, v_y_array,'r--', v_z_array, 'b--', label='x**(-3) function')
The first set of three arguments x_grid, v_y_array,'r--', v_z_array, follows this pattern:
plot(x, y, 'bo') # plot x and y using blue circle markers
The second set has only two arguments: v_z_array, 'b--', and follow this pattern:
plot(y) # plot y using x as index array 0..N-1
plot(y, 'r+') # ditto, but with red plusses
Thus, the second set is infering a sequence of x values that equals range(0, 10)(values from 0 to 9 inclusive), while the first set of arguments uses x_gridwhich equals range(1, 11) (values from 1 to 10 inclusive).
I would like to display an histogram with bars in different colors according to a condition.
I mean, I would like to set bars which are between 2 and 5 in a different color.
I've tried this:
bins = np.linspace(0, 20, 21)
lista_float_C1 = [1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,8,10,11,11]
colors = []
y = plt.hist(lista_float_C1, bins, alpha=0.5 )
for x in y[1]:
if (x >= 2)&(x=<5):
colors.append('r')
else:
colors.append('b')
print(colors)
plt.hist(lista_float_C1, bins, alpha=0.5, color = colors )
plt.show()
I get this error :
color kwarg must have one color per data set. 1 data sets and 21 colors were provided
You can modify the patches after you plot them:
lista_float_C1 = [1,1,1,2,2,2,3,4,4,5,5,6,7,8,8,8,8,10,11,11]
fig,ax = plt.subplots()
ax.hist(lista_float_C1, bins, alpha=0.5 )
for p in ax.patches:
x = p.get_height()
# modify this to fit your needs
color = 'r' if (2<=x<=5) else 'b'
p.set_facecolor(color)
plt.show()
plt.show()
Output:
If you want to color by bin values:
for p in ax.patches:
# changes here
x,y = p.get_xy()
color = 'r' if (2<=x<=5) else 'b'
p.set_facecolor(color)
plt.show()
Output:
In seaborn, how can you change just the x and y axis label font size? Instead of using the "set context" method, is there a way to specifically change just the axis labels? Here is my code:
def corrfunc(x, y, **kws):
r = stats.pearsonr(x, y)[0] ** 2
ax = plt.gca()
ax.annotate("r$^2$ = {:.2f}".format(r),
xy=(.1, .9), xycoords=ax.transAxes, fontsize=16)
if r > 0.6:
col = 'g'
elif r < 0.6:
col = 'r'
sns.regplot(x, y, color=col)
return r
IC_Plot = sns.PairGrid(df_IC, palette=["red"])
IC_Plot.map_offdiag(corrfunc)
IC_Plot.savefig("Save_Pair.png")
The easiest way to change the fontsize of all x- and y- labels in a plot is to use the rcParams property "axes.labelsize" at the beginning of the script, e.g.
plt.rcParams["axes.labelsize"] = 15
You may also set the font size of each individual label
for ax in plt.gcf().axes:
l = ax.get_xlabel()
ax.set_xlabel(l, fontsize=15)