Python matplotlib line chart learning knowledge summary


Summarize the learning about matplotlib line chart and avoid forgetting it in the future.

1. Use software

pycharm

2. Problems encountered:

matplotlib使用pycharm安装的时候,发现Python3.8会安装失败,Python3.7
安装成功,使用http://pypi.douban.com/simple/ --trusted-host pypi.douban.com/
豆瓣的镜像文件

Three, knowledge summary

1. Use matplotlib

from matplotlib import pyplot as plt

2. Set image font

plt.rcParams['font.sans-serif'] = ['SimSun']
#SimSun为你所设置的字体,如果不设置字体的话,matplotlib默认是不能显示中文的

3. Set the x, y axis data

x=range(2,26,2)   #设置x轴数据
y=[15,13,14.5,17,20,25,26,26,24,22,18,15]    #设置y轴数据
plt.plot(x,y,label="同桌",color='g',linestyle='-')
#plot中属性的作用:
#color:设置线的颜色		例如:color=‘r’等,r表示红色
#label:设置线的标签		例如:lable="自己"	
#linestyle:设置线的风格   例如:linestyle=‘-.’等
#linewidth:设置线条粗细   例如:linewidth=5
#alpha:设置透明度         例如alpha=0.5

4. Show pictures

plt.show()

5. Set the x, y axis scale

x=range(2,26,2)   #设置x轴数据
y=[15,13,14.5,17,20,25,26,26,24,22,18,15]    #设置y轴数据

#设置x轴的刻度
plt.xticks(x)

#设置y轴的刻度
plt.yticks(y)

6. Set the x-axis scale to Chinese

plt.rcParams['font.sans-serif'] = ['SimSun']
x = range(0, 120)
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
#设置_xtick_labels的范围为1000分到1100分
plt.xticks(list(x)[::5],_xtick_labels[::5],rotation=60)
#设置x轴刻度,不过x与_xtick_labels的个数必须相同

7. Set the picture size

plt.figure(figsize=(10,8),dpi=80)
#figsize:设置长,宽高度
#dpi:分辨率的大小
#图片的大小等于长,宽分别乘以dpi

8. Save the picture

plt.savefig("./t1.png")   

9. Set the x, y axis labels

plt.xlabel("时间")
plt.ylabel("温度")

10. Set the title

plt.grid(alpha=0.4)

I just started to learn data analysis, and I feel that Python has a lot of content.

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/111563086