- import numpy as np
- import random as rd
- from matplotlib import pyplot as plt
- # 產生 normal distribution 的數字
- random_nums = np.random.normal(scale=20, size=10000)
- random_ints = np.round(random_nums)
- # 計算 histogram 的相關數字
- hist, bins = np.histogram(random_ints, bins=20)
- # 繪圖
- plt.rcParams['figure.figsize'] = [15, 7]
- plt.hist(random_ints, bins)
- plt.title("histogram")
- plt.xticks(bins)
- plt.show()
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
2019年7月19日 星期五
[ Python 範例代碼 ] Matplotlib - 產生 normal distribution 的 Histogram
使用 np.random.normal 產生 random 的 normal distribution 數字, 接著使用 np.histogram 計算繪製 histogram 所需要的資料:
2019年1月26日 星期六
[ Python 常見問題 ] Plot a histogram using Matplotlib in Python with a list of data?
Source From Here
Question
I am trying to plot a histogram using the matplotlib.hist() function but I am not sure how to do it. I have a list:
and a list of names(strings).
How do I make the probability as my y-value of each bar and names as x-values?
How-To
If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have bins:
However, if you have limited number of data points, and you want a bar plot, then you may attach labels to x-axis:
Supplement
* Generate data and plot a simple histogram
Question
I am trying to plot a histogram using the matplotlib.hist() function but I am not sure how to do it. I have a list:
- probability = [0.3602150537634409, 0.42028985507246375,
- 0.373117033603708, 0.36813186813186816, 0.32517482517482516,
- 0.4175257731958763, 0.41025641025641024, 0.39408866995073893,
- 0.4143222506393862, 0.34, 0.391025641025641, 0.3130841121495327,
- 0.35398230088495575]
How do I make the probability as my y-value of each bar and names as x-values?
How-To
If you want a histogram, you don't need to attach any 'names' to x-values, as on x-axis you would have bins:
- import matplotlib.pyplot as plt
- import numpy as np
- %matplotlib inline
- x = np.random.normal(size = 1000)
- # density: If True, the first element of the return tuple will be the counts normalized to form a probability density
- plt.hist(x, density=True, bins=30)
- plt.ylabel('Probability');
However, if you have limited number of data points, and you want a bar plot, then you may attach labels to x-axis:
- x = np.arange(3)
- plt.bar(x, height= [1,2,3])
- plt.xticks(x, ['a','b','c'])
Supplement
* Generate data and plot a simple histogram
[ Python 範例代碼 ] Metplotlib - Bar chart
Source From Here
Preface
Matplotlib may be used to create bar charts. You might like the Matplotlib gallery. The course below is all about data visualization:
Bar chart code
The code below creates a bar chart:
Matplotlib charts can be horizontal, to create a horizontal bar chart:
More on bar charts
You can compare two data series using this Matplotlib code:
範例代碼 matplotlib_barchart.ipynb
Preface
Matplotlib may be used to create bar charts. You might like the Matplotlib gallery. The course below is all about data visualization:
* Data Visualization with Python and Matplotlib
Bar chart code
The code below creates a bar chart:
- %matplotlib inline
- import matplotlib.pyplot as plt; plt.rcdefaults()
- import numpy as np
- import matplotlib.pyplot as plt
- objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
- y_pos = np.arange(len(objects))
- performance = [10,8,6,4,2,1]
- plt.bar(y_pos, performance, align='center', alpha=0.5)
- plt.xticks(y_pos, objects)
- plt.ylabel('Usage')
- plt.title('Programming language usage')
- plt.show()
Matplotlib charts can be horizontal, to create a horizontal bar chart:
- import matplotlib.pyplot as plt; plt.rcdefaults()
- import numpy as np
- import matplotlib.pyplot as plt
- objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
- y_pos = np.arange(len(objects))
- performance = [10,8,6,4,2,1]
- plt.barh(y_pos, performance, align='center', alpha=0.5)
- plt.yticks(y_pos, objects)
- plt.xlabel('Usage')
- plt.title('Programming language usage')
- plt.show()
More on bar charts
You can compare two data series using this Matplotlib code:
- import numpy as np
- import matplotlib.pyplot as plt
- # data to plot
- n_groups = 4
- means_frank = (90, 55, 40, 65)
- means_guido = (85, 62, 54, 20)
- # create plot
- fig, ax = plt.subplots()
- index = np.arange(n_groups)
- bar_width = 0.35
- opacity = 0.8
- rects1 = plt.bar(index, means_frank, bar_width,
- alpha=opacity,
- color='b',
- label='Frank')
- rects2 = plt.bar(index + bar_width, means_guido, bar_width,
- alpha=opacity,
- color='g',
- label='Guido')
- plt.xlabel('Person')
- plt.ylabel('Scores')
- plt.title('Scores by person')
- plt.xticks(index + bar_width, ('A', 'B', 'C', 'D'))
- plt.legend()
- plt.tight_layout()
- plt.show()
範例代碼 matplotlib_barchart.ipynb
訂閱:
文章 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
CNN 卷積神經網路簡介 STEP1. 卷積神經網路介紹 CNN 卷積神經網路可以分成兩大部分: * 影像的特徵提取 : 透過 Convolution 與 Max Pooling 提取影像特徵. * Fully connected Feedforward n...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...