python绘制对数坐标图描点_用Matplotlib在对数刻度上绘制直方图

I have a pandas DataFrame that has the following values in a Series

x = [2, 1, 76, 140, 286, 267, 60, 271, 5, 13, 9, 76, 77, 6, 2, 27, 22, 1, 12, 7, 19, 81, 11, 173, 13, 7, 16, 19, 23, 197, 167, 1]

I was instructed to plot two histograms in a Jupyter notebook with Python 3.6. No sweat right?

x.plot.hist(bins=8)

plt.show()

I chose 8 bins because that looked best to me.

I have also been instructed to plot another histogram with the log of x.

x.plot.hist(bins=8)

plt.xscale('log')

plt.show()

This histogram looks TERRIBLE. Am I not doing something right? I've tried fiddling around with the plot, but everything I've tried just seems to make the histogram look even worse. Example:

x.plot(kind='hist', logx=True)

I was not given any instructions other than plot the

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要使用Python画等高线,并使y轴具有对数刻度,可以按照以下步骤操作: 1. 导入必要的库,包括numpy和matplotlib。 2. 生成网格数据。可以使用numpy中的meshgrid函数生成网格数据。 3. 计算Z值。使用公式或其他方法计算每个网格点的Z值。 4. 绘制等高线。使用matplotlib中的contour函数绘制等高线。可以设置参数来控制等高线的数量、颜色和标签等。 5. 设置y轴为对数刻度。使用matplotlib中的semilogy函数设置y轴为对数刻度。 下面是一个简单的例子代码: ``` python import numpy as np import matplotlib.pyplot as plt # 生成网格数据 x = np.linspace(-5, 5, 100) y = np.linspace(0, 10, 100) X, Y = np.meshgrid(x, y) # 计算Z值 Z = np.sin(X) * np.exp(-Y/2) # 绘制等高线 plt.contour(X, Y, Z, levels=10, cmap='jet') # 设置y轴为对数刻度 plt.semilogy() # 显示像 plt.show() ``` 这段代码将生成一个具有对数刻度的等高线,其中Y轴从0开始,以10为底数进行刻度。 ### 回答2: 要使用Python绘制带有对数刻度的等高线,我们可以借助matplotlib库来完成。下面是一个简单的示例代码: ``` python import matplotlib.pyplot as plt import numpy as np # 生成示例数据 x = np.linspace(0, 10, 100) y = np.logspace(0, 10, 100) X, Y = np.meshgrid(x, y) Z = np.sin(X) + np.cos(Y) # 绘制等高线 plt.contour(X, Y, Z) # 设置y轴为对数刻度 plt.yscale('log') # 添加色标 plt.colorbar() # 添加标题和轴标签 plt.title('Contour plot with logarithmic y-axis') plt.xlabel('x') plt.ylabel('y') # 显示形 plt.show() ``` 在这个示例中,我们首先使用numpy库生成了一组示例数据,然后使用contour函数绘制了等高线。接着,我们使用yscale函数将y轴设置为对数刻度,并添加了色标以表示等高线值的范围。最后,我们添加了标题和轴标签,并使用show函数来显示形。运行以上代码,即可获得一个带有对数刻度的等高线。 ### 回答3: 要使用Python绘制带有对数刻度的等高线,可以使用matplotlib库。 首先,需要导入所需库。在Python中,导入matplotlib.pyplot和numpy库,可以使用以下代码实现: ``` import matplotlib.pyplot as plt import numpy as np ``` 接下来,生成数据并创建网格坐标。可以使用numpy的meshgrid函数生成网格坐标,并使用numpy的logspace函数生成对数刻度的y轴数据。代码示例如下: ``` x = np.linspace(-5, 5, 100) y = np.logspace(0, 2, 100) X, Y = np.meshgrid(x, y) ``` 然后,计算z轴数据。可以根据具体需求编写函数来计算z轴的数值。以下是一个简单例子,在这个例子中,z的值是x和y的线性组合: z = x + y。 ``` Z = X + Y ``` 最后,绘制等高线并设置y轴为对数刻度。可以使用matplotlib.pyplot的contour函数绘制等高线,并使用matplotlib.pyplot的semilogy函数将y轴刻度设置为对数刻度。 ``` plt.contour(X, Y, Z) plt.semilogy() plt.show() ``` 完成以上步骤后,运行代码即可生成带有对数刻度的等高线。这是一个简单的示例,可以根据具体需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值