Matplotlib Pyplot Plt Python Pandas Data Visualization Plotting
1 2 3 4 5 |
import matplotlib.pyplot as plt %matplotlib inline # simple syntax is matplotlib plots as plt.plot(x, y) plt.plot([1,2,3], [5,6,7]) plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# plotting with variables and basic labelling of our plot x = [1,2,3] y = [5,6,7] ##adding a legend to the plot #add another line vectors first x2 = [4,6,8] y2= [10,14,18] plt.plot(x, y, label='First Line') # you have to give the plots a label before you can call the legends on them plt.plot(x2, y2, label='Second Line') plt.xlabel('Plot number') plt.ylabel('Important var') plt.title('Plotting\nNice Graph') #\nGives you a sub title plt.legend() #call the legend plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#plotting barcharts and histograms x3 = [2,4,6,8,10] y3 = [3,5,7,9,11] #second set of bars x4 = [1,3,5,7,9] y4 = [2,6,6,12,4] #plot barchart plt.bar(x3, y3, label='Bar1', color='r') plt.bar(x4,y4, label='Bar2', color ='c') plt.xlabel('x3') plt.ylabel('y3') plt.title('Bar Chart') plt.legend() plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#plotting histogram. population_dist = [24,45,5,6,42,65,34,75,75,34,75,7,24,26,87,98,98,67,86, 65,63, 87,98,76,53,35] #ids = [x for x in range(len(population_dist))] for bar chart plotting #plot barchart #plt.bar(ids, population_dist, label='hist') #histogram works with bins so we will have to create bin 'containers' for our distribution bins = [10,20,30,40,50,60,70,80,90,100] plt.hist(population_dist, bins, histtype='bar', rwidth=0.5) plt.xlabel('x3') plt.ylabel('y3') plt.title('Histogram') plt.legend() plt.show() |
1 2 3 4 5 6 7 8 9 10 11 |
## scatter plot. #basic skeletion x = [1,2,3,4,5,6,7,8,9] y = [4,5,8,3,6,9,5,4,8] plt.scatter(x,y, label='ScatterShow', color='c', marker='^' , s=60) # s = size of the marker plt.xlabel('x3') plt.ylabel('y3') plt.title('Scatter') plt.legend() plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#stacked plot days = [1, 2, 3, 4, 5] sleeping = [6,8,7,3,8] eating = [6,9,7,3,6] working = [9,8,5,7,8] #working around legends for stackplots plt.plot([],[], color='red', label='eating', linewidth=2) plt.plot([],[], color='blue', label='sleeping', linewidth=2) plt.plot([],[], color='yellow', label='working', linewidth=2) plt.stackplot(days, eating, sleeping, working, color=['red','blue','yellow']) plt.xlabel('x3') plt.ylabel('y3') plt.title('Stacked Plot') plt.legend() plt.show() |
1 2 3 4 5 6 7 8 9 10 |
#pie charts days = [1, 2, 3, 4, 5] sleeping = [6,8,7,3,8] eating = [6,9,7,3,6] working = [9,8,5,7,8] slices = [2,4,6,8,1] colors = ['red','blue','green','brown','pink'] activities = ['sleeping','eating','working','dancing','travelling'] plt.pie(slices, labels=activities, colors=colors, startangle=90, shadow=True, explode=(0,0.1,0,0,0), autopct='%1.1f%%') |
In [48]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#modifying the matplotlib #plotting barchart. x3 = [2,4,6,8,10] y3 = [3,5,7,9,11] #second set of bars x4 = [1,3,5,7,9] y4 = [2,6,6,12,4] fig = plt.figure(facecolor='#f0f0f0') #set facecolour or background colour which will be maintained once we save our graph ax1 = plt.subplot2grid((1,1),(0,0)) #tilt the xlabels for labels in ax1.xaxis.get_ticklabels(): labels.set_rotation(45) #plot barchart ax1.bar(x3, y3, label='Bar1', color='r') ax1.bar(x4,y4, label='Bar2', color ='c') ax1.grid(True) #add a grid plt.xlabel('x3') plt.ylabel('y3') plt.title('Bar Chart') plt.legend() #customise the legend leg = ax1.legend(loc=2,ncol=6, prop={'size':10}) leg.get_frame().set_alpha(0.4) ##define spacing around the ticks and labels plt.subplots_adjust(left = 0.09, bottom=0.20, right=0.94, top= 0.93, wspace = 0.2, hspace = 0) plt.show() #lets save the graph fig.savefig('matplotlib.png', facecolor=fig.get_facecolor()) |
I see your website is in the same niche like my weblog.
Do you allow guest posting? I can write interesting & unique posts for you.
Let me know if you are interested.
Hi Norma91,
Yes, I do allow guest posting. You can forward guest articles to siradaba [at] yahoo [dot] co.uk
Please put “Guest Post” as part of the email subject so it can be spotted quickly.
Many thanks
Ben