# Python draws a curve experiment with matplotlib

2022-08-06 05:02:02

Calculate the mean and label difference for each set of data,用matplotlibDraw the following style.The horizontal axis is the serial number of each group,即1,2,… 10,The dots of the curve correspond to the mean,The color range is marked difference,Displays vertical and horizontal gridlines.

First analyze the needs of this topic：An image needs to be drawn from the given data,And the mean value is also marked with a small dot,Also have vertical and horizontal 的网格线,There is also the standard deviation of the color range,Also have the label in the upper left corner of the image.

In the specific implementation, first convert the two-dimensional list of stored data into numpy的数组,之后用一个std列表和一个avgThe list stores the standard deviation and mean of each set of data separately.Then observe that the discount is constructed by subscripting and averaging,于是在plt.plotThe incoming is from1到10的x和avg,颜色设置为灰色,Try to match the topic as much as possible,The dot color is set to black,The solid line width is 3,标签记为Mean Value,The image drawn at this time does not have a color range.We are passing oney1Lists are marked as superscripts of colors,具体值就是avgplus one-halfstd,y2的值记做avgSubtract one-half,之后通过fill_betweenDraw this range into the image,Set the color to blue,label to doStandard Deviation.At this time, it is found that there is no label when outputting,是因为没有调用plt.legend(loc=2),After adding, the output gets the correct result.

``````import numpy as np
import matplotlib.pyplot as plt
import matplotlib
list=[[1,2,3,4,5,6],[2,4,6,8,10,12],[1,3,5,7,9,11],[1.1,2.2,3.3,4.4,5.5,6.6],[3,6,9,12,15,18],[4,8,12,16,20,24
],[5,10,15,20,25,30],[1.5,2.5,3.5,4.5,5.5,6.5],[1,4,7,10,13,16],[2,5,8,11,14,17]]
a=np.array(list)
std=[]
avg=[]
x=[1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(list)):
std.append(np.std(a[i]))
avg.append(np.mean(a[i]))
fig=plt.figure()
plt.xticks(x,x)
plt.plot(x,avg,marker="o",color='#858687',markeredgecolor='black',linewidth=3,label="Mean Value")
for i in range(len(avg)):
plt.text(x[i],avg[i],avg[i],fontsize=10)
y1=[]
y2=[]
for i in range(len(x)):
y1.append(avg[i]+std[i]/2)
y2.append(avg[i] - std[i] / 2)
plt.fill_between(x,y1,y2,color='#d2e3f0',label="Standard deviation")
plt.grid()
plt.legend(loc=2)
plt.show()
``````

copyright notice
author[Chloroplasts do not forget to breathe],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/218/202208060500254178.html