This is my function to create box plot. But it does not show the outliers. any help?
def box_plot_draw (data, box_colors):
#plt.style.use('Solarize_Light2')
fig, ax = plt.subplots()#figsize=(10, 6))
fig.canvas.set_window_title('A Boxplot Example')
fig.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25) #
bp = ax.boxplot(data, notch=0, sym='+', vert=1, whis=1.5)
plt.setp(bp['boxes'], color='black')
plt.setp(bp['whiskers'], color='black')
plt.setp(bp['fliers'], color='red', marker='+')
box_colors = box_colors
ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) #what is which?
ax.set_axisbelow(True)
#setting the tile for distribution
ax.set_title('Comparison of the duration of time for two Distributions')
ax.set_xlabel('Distribution')
ax.set_ylabel('Value')
You can test the code using this data.
np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
## combine these different collections into a list
data_to_plot = [collectn_1, collectn_2]
box_colors = ['black', 'green']
box_plot_draw (data_to_plot, box_colors)
plt.show()
I am using the Jupyter notebook and I used styles for other functions. But I did not use any style for this function.
Related
I am plotting a graph with 3 y axis, and it shows in the plot window of Spyder instead of saving correctly in a folder. The graph saved in a folder is blank.
Here's my code :
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twinx()
plt.figure(figsize = (121, 75))
ax1.set_xlabel('time(s)')
ax1.set_ylabel('y1')
ax2.set_ylabel('y2')
ax3.set_ylabel('y3')
rspine = ax3.spines['right']
rspine.set_position(('axes', 1.15))
plt.xticks(fontsize=50)
plt.yticks(fontsize=50)
ax1.plot(fd[fd.columns[0]], fd[fd.columns[1]], color = 'r', linewidth = 2)
ax2.plot(fd[fd.columns[0]], fd[fd.columns[2]], color = 'g', linewidth = 2)
ax3.plot(fd[fd.columns[0]], fd[fd.columns[3]], color = 'b', linewidth = 2)
ax3.legend([ax1.get_lines()[0], ax2.get_lines()[0], ax3.get_lines()[0]], ['y1', 'y2', 'y3'])
plt.savefig(folder+"/"+str(file_name)+"y1/2/3.png")
plt.close()
Am I doing something wrong here ? I don't call the show() function so I don't understand why the graph shows anyway (when plotting the 3 y axis on the same graph with only using plt.figure(), plt.plot() then plt.save() and close(), it works as intended)
Edit : What I want is to have the graph saved correctly in my folder, with the size I set in plt.figure(), which isn't the case now.
I found what went wrong :
I stead of writing
fig = plt.figure(figsize=(121, 75)) fig, ax1 = plt.subplots(),
I had to write
fig, ax1 = plt.subplots(figsize=(121, 75))
The graph then plots at the size indicated, and doesn't shows in the window of Spyder.
You can use set_size_inches before saving the figure. dpi feature can be use to increase the resolution of the figure also.
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.size'] = '50'
fig = plt.figure(figsize=(121, 75), dpi=200)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twinx()
ax1.set_xlabel('time(s)')
ax1.set_ylabel('y1')
ax2.set_ylabel('y2')
ax3.set_ylabel('y3')
rspine = ax3.spines['right']
rspine.set_position(('axes', 1.05))
rspine2 = ax2.spines['right']
rspine2.set_position(('axes', 1.0))
# plt.xticks(fontsize=50)
# plt.yticks(fontsize=50)
ax1.plot(np.arange(100), np.random.rand(100), color='r', linewidth=2)
ax2.plot(np.arange(100), np.random.rand(100), color='g', linewidth=2)
ax3.plot(np.arange(100), np.random.rand(100), color='b', linewidth=2)
ax3.legend([ax1.get_lines()[0], ax2.get_lines()[0], ax3.get_lines()[0]], ['y1', 'y2', 'y3'])
# fig.tight_layout()
figure = plt.gcf()
figure.set_size_inches(121, 75)
plt.savefig("3.png", dpi=None)
plt.close()
using matplotlib in Jupyter Notebook, I have already written code to produce a number of different graphs (example code below used to create some of my graphs). I would like to put all of these graphs into a dashboard. Is there a preferred method for my instance where I already have the graph produced and just want to plug it in to a dashboard template?
It seems like there are a number of different options (bokeh and plotly).
# multiple line plots
plt.plot( 'Week', 'ESI_1_2_3', data=SZ_ED_TOT, marker='o', color='blue', linewidth=2)
plt.plot( 'Week', 'ESI_4_5', data=SZ_ED_TOT, marker='o', color='red', linewidth=2)
plt.ylabel('Patient Count')
plt.xlabel('Week')
plt.xticks(rotation = 90)
plt.title('SZ Acuity Counts')
plt.gcf().set_size_inches(20, 8)
plt.legend()
plt.show()
# multiple line plots
plt.plot( 'Week', '%_1_2_3', data=UMC_ED_TOT, marker='o', color='blue', linewidth=2)
plt.plot( 'Week', '%_4_5', data=UMC_ED_TOT, marker='o', color='red', linewidth=2)
plt.ylabel('% of Patients')
plt.xlabel('Week')
plt.xticks(rotation = 90)
plt.title('UMC Acuity % Of Total Patients')
plt.gcf().set_size_inches(20, 8)
plt.legend()
plt.show()
#dual Y axis graph
ax = UMC_Cobmined.plot.bar(x='Week', y='Total', rot=90,color=(0.2, 0.4, 0.6, 0.6))
ax.set_xlabel('Week')
ax.set_ylabel('Total Volume')
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(),
UMC_Cobmined[['%_ESI_3','%_ESI_4','%_ESI_5']].values,
linestyle='-',
marker='o', linewidth=2.0)
ax2.set_ylabel('% LWBS By Acuity')
ax2.legend(["%_ESI_3", "%_ESI_4","%_ESI_5"]);
plt.title('UMC LWBS Acuity % by Volume')
plt.gcf().set_size_inches(20, 8)
plt.show()
Ideal example of what I would like my dashboard to look like:
You can achieve this by using a grid in matplotlib:
import matplotlib.pytplot as plt
fig = plt.figure()
grid = fig.add_gridspec(NUMBER_OF_GRID_COLUMNS, NUMBER_OF_GRID_ROWS)
and then add each plot to an axis:
ax1 = fig.add_subplot(grid[0, 0])
# add plot to ax1
ax1.plot(x,y)
ax1.title("Plot on ax1")
ax2 = fig.add_subplot(grid[1,0])
# add plot to ax2
ax2.plot(x,y)
ax2.title("Plot on ax2")
plt.show()
and so on
I am trying to make this animated so that the dot and the green line move due to the for loop. This code displays 3 different graphs one below the other. The middle graph has no animation section.
x =lag_range
count = 0
plt.ion()
fig, ax = plt.subplots()
for b in x:
plt.subplot(311)
plt.plot(x,pear_corr, color='b', linewidth=1.5, label ='Pearson')
plt.plot(x,spear_corr, color ='r', linewidth=1.5, label='Spearman')
plt.plot(x[count],pear_corr[count],'yo')
plt.legend()
axes = plt.gca()
plt.ylabel('Correlation coefficients')
plt.xlabel('Lag times /days')
axes.set_xlim([min(lag_list),last])
axes.set_ylim(-1,1)
plt.subplot(312)
plt.plot(x,pear_p_values, color='b', linewidth=1.5)
plt.plot(x,spear_p_values, color ='r', linewidth=1.5)
axes = plt.gca()
plt.ylabel('P values')
plt.xlabel('Lag times /days')
axes.set_xlim([min(lag_list),last])
plt.subplot(313)
ax1 = plt.subplot(313)
x_for_p = range(len(x_prices))
ax1.plot(x_for_p, x_prices, color ='grey', linewidth=1.5)
ax1.set_ylabel('Share price', color ='grey')
ax1.tick_params('y', colors='grey')
ax1.set_xlabel('Days')
axes = plt.gca()
axes.set_xlim([min(lag_list),(2*last)])
ax2 = ax1.twinx()
x_for_den = range(b,(b+len(x_prices)))
ax2.plot(x_for_den, y_planes, color='g', linewidth=1.5)
ax2.set_ylabel('Plane density', color='g')
ax2.tick_params('y', colors='g')
count += 1
plt.pause(2)
plt.draw()
cross_corr2_vis(prices, density_p3)
If you could share a working code or just definitions of variables pear_corr, spear_corr, etc., the following code might have not resulted in this simple animation:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
N_points = 1000
x = np.linspace(0,2.*np.pi,N_points)
y = np.sin(x)
fig, ax = plt.subplots()
ax.set_xlim([0,2.*np.pi])
ax.set_ylim([-1,1])
line, = ax.plot( [],[], lw=2, color='g')
sctr = ax.scatter([],[], s=100, color='r')
def animate(i):
line.set_ydata(y[:i+1]) # update
line.set_xdata(x[:i+1])
sctr.set_offsets((x[i],y[i]))
return line,sctr
ani = animation.FuncAnimation(fig, animate, N_points, interval=5, blit=True)
plt.show()
I am trying to plot one or more lines on the same chart as a bar chart to show a few different metrics. I heard I should use ax.twinx() for this, but I either get an error saying x and y must have the same first dimension, or a different error reading 0L based on the two things I tried. Here is my code;
x = df4['Date']
y = df4['Rate']
ax = df4[['Date','Qty']].set_index('Date') \
.plot(kind='bar',
stacked=False,
color = 'dodgerblue',
figsize=(13,4),
legend=True)
ax.set_xlabel(r"$\rm \bf{Date}$",
fontsize=18,
rotation = 0)
ax.set_ylabel(r'$\cal \bf{Qty}$ ',fontsize=18, rotation = 90)
ax2 = ax.twinx()
ax2.plot(x, y, color = 'green', linestyle = '--', linewidth= 2.0)
Note; df4 is a grouped pandas dataframe. Not sure how relevant that is but just in case.
Try this:
fig, ax = plt.subplots()
ax2 = ax.twinx()
df4[['Date','Qty']].set_index('Date') \
.plot(kind='bar',
ax=ax,
color = 'dodgerblue',
figsize=(13,4),
legend=False)
patches, labels = ax.get_legend_handles_labels()
ax.legend(patches, labels, loc='upper left')
ax.set_xlabel(r"$\rm \bf{Date}$", fontsize=18, rotation=0)
ax.set_ylabel(r'$\cal \bf{Qty}$ ',fontsize=18, rotation=90)
ax2.plot(range(len(df4)), df4['Rate'], 'green', label='Rate',
linestyle = '--', linewidth=2.0)
patches, labels = ax2.get_legend_handles_labels()
ax2.legend(patches, labels, loc='upper right')
NOTE: if you want a tested solution, please provide a sample data
How do we draw an average line (horizontal) for a histogram in using matplotlib?
Right now, I'm able to draw the histogram without any issues.
Here is the code I'm using:
## necessary variables
ind = np.arange(N) # the x locations for the groups
width = 0.2 # the width of the bars
plt.tick_params(axis='both', which='major', labelsize=30)
plt.tick_params(axis='both', which='minor', labelsize=30)
ax2 = ax.twinx()
## the bars
rects1 = ax.bar(ind, PAAE1, width,
color='0.2',
error_kw=dict(elinewidth=2,ecolor='red'),
label='PAAE1')
rects2 = ax.bar(ind+width, PAAE2, width,
color='0.3',
error_kw=dict(elinewidth=2,ecolor='black'),
label='PAAE2')
rects3 = ax2.bar(ind+width+width, AAE1, width,
color='0.4',
error_kw=dict(elinewidth=2,ecolor='red'),
label='AAE1')
rects4 = ax2.bar(ind+3*width, AAE2, width,
color='0.5',
error_kw=dict(elinewidth=2,ecolor='black'),
label='AAE3')
maxi = max(dataset[2])
maxi1 = max(dataset[4])
f_max = max(maxi, maxi1)
lns = [rects1,rects2,rects3,rects4]
labs = [l.get_label() for l in lns]
ax.legend(lns, labs, loc='upper center', ncol=4)
# axes and labels
ax.set_xlim(-width,len(ind)+width)
ax.set_ylim(0, 100)
ax.set_ylabel('PAAE', fontsize=25)
ax2.set_ylim(0, f_max+500)
ax2.set_ylabel('AAE (mW)', fontsize=25)
xTickMarks = dataset[0]
ax.set_xticks(ind+width)
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=90, fontsize=25)
I want to plot the average line for PAAE 1, 2 and AAE 1, 2.
What should I be using to plot the average line?
If you'd like a vertical line to denote the mean use axvline(x_value). This will place a vertical line that always spans the full (or specified fraction of) y-axis. There's also axhline for horizontal lines.
In other works, you might have something like this:
ax.axvline(data1.mean(), color='blue', linewidth=2)
ax.axvline(data2.mean(), color='green', linewidth=2)
As a more complete, but unnecessarily complex example (most of this is nicely annotating the means with curved arrows):
import numpy as np
import matplotlib.pyplot as plt
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(-2, 1.5, 1000)
fig, ax = plt.subplots()
bins = np.linspace(-10, 5, 50)
ax.hist(data1, bins=bins, color='blue', label='Dataset 1',
alpha=0.5, histtype='stepfilled')
ax.hist(data2, bins=bins, color='green', label='Dataset 2',
alpha=0.5, histtype='stepfilled')
ax.axvline(data1.mean(), color='blue', linewidth=2)
ax.axvline(data2.mean(), color='green', linewidth=2)
# Add arrows annotating the means:
for dat, xoff in zip([data1, data2], [15, -15]):
x0 = dat.mean()
align = 'left' if xoff > 0 else 'right'
ax.annotate('Mean: {:0.2f}'.format(x0), xy=(x0, 1), xytext=(xoff, 15),
xycoords=('data', 'axes fraction'), textcoords='offset points',
horizontalalignment=align, verticalalignment='center',
arrowprops=dict(arrowstyle='-|>', fc='black', shrinkA=0, shrinkB=0,
connectionstyle='angle,angleA=0,angleB=90,rad=10'),
)
ax.legend(loc='upper left')
ax.margins(0.05)
plt.show()