为什么python中柱状图是一条线
时间: 2024-06-07 22:11:23 浏览: 5
在Python中,如果使用Matplotlib库的默认绘图函数`plot()`来绘制柱状图,会出现柱状图变成一条线的情况。这是因为`plot()`函数默认绘制的是折线图,而不是柱状图。要绘制柱状图,应该使用Matplotlib库中的`bar()`函数或者`barh()`函数,它们专门用于绘制柱状图。此外,还可以使用Seaborn库中的`barplot()`函数来绘制柱状图。
相关问题
怎么利用pyqtgraph实现三维建模中的扫描操作,即根据一条曲线和面建立三维模型,比如根据一条直线和圆建立圆柱三维模型,请给出相应代码。
要实现三维建模中的扫描操作,可以使用Python中的pyqtgraph库,该库提供了一些用于图形计算的函数和类。以下是一个简单的示例代码,它可以根据一条直线和圆建立圆柱三维模型:
```
import pyqtgraph.opengl as gl
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow
# 创建主窗口
app = QApplication([])
window = QMainWindow()
# 创建一个OpenGL窗口
view = gl.GLViewWidget()
window.setCentralWidget(view)
# 创建一个三维坐标轴
axis = gl.GLAxisItem()
view.addItem(axis)
# 创建圆柱的高度和半径
height = 2
radius = 1
# 创建圆上的点
theta = np.linspace(0, 2*np.pi, 50)
x = radius*np.cos(theta)
y = radius*np.sin(theta)
z = np.zeros_like(x)
points = np.vstack([x, y, z]).transpose()
# 创建圆上的线段
lines = []
for i in range(len(theta)-1):
lines.append([i, i+1])
lines.append([len(theta)-1, 0])
# 创建圆柱侧壁上的面
faces = []
for i in range(len(theta)-1):
face = [i, i+1, i+len(theta)+1, i+len(theta)]
faces.append(face)
face = [len(theta)-1, 0, len(theta), len(theta)*2-1]
faces.append(face)
# 创建圆柱底面和顶面的面
bottom = np.zeros_like(points)
bottom[:, 2] = -height/2
top = np.zeros_like(points)
top[:, 2] = height/2
faces_bottom = []
faces_top = []
for i in range(len(theta)-1):
face_bottom = [i, i+1, len(theta)]
face_top = [i+len(theta), i+len(theta)+1, len(theta)*2-1]
faces_bottom.append(face_bottom)
faces_top.append(face_top)
face_bottom = [len(theta)-1, 0, len(theta)]
face_top = [len(theta)*2-1, len(theta), len(theta)*2-2]
faces_bottom.append(face_bottom)
faces_top.append(face_top)
# 创建一个MeshItem对象
mesh = gl.GLMeshItem(vertexes=np.vstack([points, bottom, top]), faces=np.vstack([faces, faces_bottom, faces_top]), drawEdges=True)
# 添加MeshItem对象到OpenGL窗口中
view.addItem(mesh)
# 显示窗口
window.show()
app.exec_()
```
在上面的代码中,我们首先创建了一个主窗口和一个OpenGL窗口,在OpenGL窗口中添加一个三维坐标轴。接着,我们创建圆柱的高度和半径,并使用这些参数创建圆上的点和线段,以及圆柱侧壁上的面、底面和顶面的面。最后,我们创建一个GLMeshItem对象,将其添加到OpenGL窗口中,并显示窗口。
需要注意的是,上述代码仅为示例代码,实际应用中还需要根据具体场景进行相应的修改和优化。同时,pyqtgraph库还提供了许多其他的函数和类,可以用于实现更加复杂的三维建模操作。
python 正态分布拟合
在Python中,可以使用numpy和matplotlib库来进行正态分布的拟合。以下是三个示例代码,分别展示了不同的方法来绘制正态分布的直方图和拟合曲线。
示例代码1:
```python
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
def demo2():
mu, sigma, num_bins = 0, 1, 50
x = mu + sigma * np.random.randn(1000000) # 生成正态分布的数据
n, bins, patches = plt.hist(x, num_bins, normed=True, facecolor='blue', alpha=0.5) # 绘制直方图
y = mlab.normpdf(bins, mu, sigma) # 拟合曲线
plt.plot(bins, y, 'r--') # 绘制拟合曲线
plt.xlabel('Expectation')
plt.ylabel('Probability')
plt.title('Histogram of normal distribution: $\mu = 0$, $\sigma=1$')
plt.subplots_adjust(left=0.15)
plt.show()
demo2()
```
示例代码2:
```python
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
def demo1():
mu, sigma = 0, 1
sampleNo = 1000
np.random.seed(0)
s = np.random.normal(mu, sigma, sampleNo)
plt.hist(s, bins=100, normed=True)
plt.show()
demo1()
```
示例代码3:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import seaborn as sns
sns.set(context='notebook', font='simhei', style='whitegrid') # 设置风格尺度和显示中文
import warnings
warnings.filterwarnings('ignore') # 不发出警告
def demo3():
s = np.loadtxt('D:/dis.txt') # 从文件中读取数据
mu = np.mean(s) # 计算均值
sigma = np.std(s) # 计算标准差
num_bins = 50 # 直方图柱子的数量
n, bins, patches = plt.hist(s, num_bins, normed=1, facecolor='blue', alpha=0.5) # 绘制直方图
y = norm.pdf(bins, mu, sigma) # 拟合一条最佳正态分布曲线
plt.plot(bins, y, 'r--') # 绘制拟合曲线
plt.xlabel('sepal-length') # 绘制x轴
plt.ylabel('Probability') # 绘制y轴
plt.title(r'Histogram: $\mu={}$, $\sigma={}$'.format(mu, sigma)) # 在标题中显示mu和sigma
plt.subplots_adjust(left=0.15) # 调整左边距
plt.show()
demo3()
```
这些示例代码展示了如何使用numpy和matplotlib库来绘制正态分布的直方图和拟合曲线。你可以根据自己的需求选择其中的一种方法来进行正态分布的拟合。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)