python3-matplotlib绘制散点图、绘制条形图
生活随笔
收集整理的這篇文章主要介紹了
python3-matplotlib绘制散点图、绘制条形图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
matplotlib 支持的圖形 https://matplotlib.org/stable/gallery/index.html
1、繪制散點圖
from matplotlib import pyplot as plt from matplotlib import font_manager# y_3是三月每天的最高溫度 y_10 是十月每天的最高溫度 y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23] y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,10,11,9,3]x_3 = range(1,32) x_10 = range(51,82)my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SIMLI.TTF", size=12)# 設置圖形大小 plt.figure(figsize=(15, 8), dpi=80)# 使用 scatter繪制散點圖,和繪制折線圖方法唯一區別就是 調用方法不同 plt.scatter(x_3, y_3, label="3月份") plt.scatter(x_10, y_10, label="10月份")# 調整x軸的刻度 _x = list(x_3) + list(x_10) xtick_labels = ["3月{}日".format(i) for i in x_3] xtick_labels += ["10月{}日".format(i-50) for i in x_10] plt.xticks(_x[::3],xtick_labels[::3],fontproperties=my_font,rotation=45)# 添加圖例 plt.legend(loc="upper left",prop=my_font)# 添加描述信息 plt.xlabel("時間",fontproperties=my_font) plt.ylabel("溫度",fontproperties=my_font) plt.title("標題",fontproperties=my_font)# 展示 plt.show()2、繪制條形圖
繪制豎向條形圖
from matplotlib import pyplot as plt from matplotlib import font_manager# 展示排行前20的電影 名字和票房# 票房 y = [56.01, 26.94, 17.53, 16.49, 15.45, 12.96, 11.8, 11.61, 11.28, 11.12, 10.49, 10.3, 8.75, 7.55, 7.32, 6.99, 6.88, 6.86, 6.58, 6.23] # 電影 x = ["戰狼{}".format(i) for i in range(len(y))]# my_font= font_manager.FontProperties(fname=r"C:\Windows\Fonts\SIMLI.TTF", size=12) my_font= font_manager.FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc", size=12)# 設置圖片大小 plt.figure(figsize=(15, 8), dpi=80)# 生成豎向的條形圖 width 是條形圖的寬度 plt.bar(range(len(x)),y,width=0.5)plt.xticks(range(len(x)), x, fontproperties=my_font,rotation=45)plt.show()
繪制橫向條形圖
繪制多條條形圖
3、其他
更多 matplotlib 支持圖形請參考: https://matplotlib.org/stable/gallery/index.html
前端更多圖形,可以參考echarts:
https://echarts.apache.org/examples/zh/index.html
比 matplotlib更牛的可視化神器 Plotly,https://plotly.com/python/
seaborn是類似matplotlib 的工具: https://seaborn.pydata.org/
https://www.bilibili.com/video/BV1hx411d7jb?p=9
https://www.bilibili.com/video/BV1hx411d7jb?p=10
https://www.bilibili.com/video/BV1hx411d7jb?p=11
總結
以上是生活随笔為你收集整理的python3-matplotlib绘制散点图、绘制条形图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: # 学习使用计算机
- 下一篇: JavaScript从入门到放弃 -(三