python - 即使我没有相关数据,如何增加概率密度图中 x 轴的范围?
问题描述
实际上我正在尝试在 python 中绘制密度图。我想要 -1 到 1 的范围,但是我知道我的数据集中没有超出 -0.6 和 0.6 的值。但是有什么方法可以让所有超出-0.6和0.6的值都绘制为零。简而言之,我想增加我的情节范围以使其保持一致。
到目前为止,我正在使用此代码:
import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
data_Pre1 = pd.read_csv("new errors.csv")
for s in data_Pre1.columns:
data_Pre1[s].plot(kind='density', sharey = True)
#plt.title("Disk Galaxies", fontsize = 18)
plt.xlabel("$E_i$", fontsize = 40)
plt.ylabel('Density', fontsize = 40)
plt.xlim(-1,1)
plt.legend(fontsize =25)
plt.xticks(size = 15)
plt.yticks(size = 15)
plt.show()
解决方案
我的解决方案是基于将ind参数传递给plot。它为估计的 PDF 指定评估点。我选择了700的点数,但您可以根据需要更改它,例如获得更平滑的曲线。为了保持一致性,将相同的边框值传递给plt.xlim(...)。
因此,将代码的相应行更改为:
minX, maxX = (-1.0, 1.0)
for s in data_Pre1.columns:
data_Pre1[s].plot(kind='density', sharey=True, ind=np.linspace(minX, maxX, 700))
plt.xlim(minX, maxX)
其他可能的更正是,您可以为整个DataFrame 调用绘图,而不是显式循环遍历 DataFrame 的列:
data_Pre1.plot.density(ind=np.linspace(minX, maxX, 700))
编辑
用ind指定的评估点不需要在整个x轴“需要”范围内均匀分布。
如果您确定绘图函数“发现”的x轴的两个“限制”(您编写了-0.6和0.6),则可以仅在此范围内将ind生 成为密集间隔点,然后:
- 在它前面加上一个点- 你的“想要的”下x限制,
- 将它附加一个点- 你的“想要的”上限x限制。
因此,您可以将代码更改为:
minX, maxX = (-1.0, 1.0) # "Wanted" x axis limits
minSrc, maxSrc = (-0.6, 0.6) # X axis limits "discovered" in your data
nPoints = 700 # How many estimation points in the "discovered" range
ind = np.concatenate(([minX], np.linspace(minSrc, maxSrc, nPoints), [maxX]))
data_Pre1.plot.density(ind=ind)
plt.xlim(minX, maxX)
推荐阅读
- c++ - 具有固定大小数组成员的结构的 C++ 大括号初始化
- java - 使用 launch4j 将 .jar 转换为 .exe
- php - 将数据库查询结果转换为 excel 并允许从 ajax 调用下载
- java - 如何恢复损坏的excel文件
- javascript - 在点击旁边弹出具有绝对位置的div
- javascript - 如果文本是数字,如何将函数应用于所有标签内的文本?
- c# - Azure 函数可选“中间”路由参数
- dictionary - 我如何在java中阅读镶木地板字典
- asp.net-mvc - Syteline 8 中的自定义网页
- sql - 从打包的 .NET 工具运行请求时查询超时已过期