简体   繁体   English

即使我没有数据,如何增加概率密度 plot 的 x 轴范围?

[英]How can I increase range of x-axis in probability density plot even If I have no data for that?

Actually I am trying to plot density plot in python.实际上我正在尝试 python 中的 plot 密度 plot。 I want range for -1 to 1 however I know I don't have values in my data set beyond -0.6 and 0.6.我想要 -1 到 1 的范围,但是我知道我的数据集中没有超出 -0.6 和 0.6 的值。 But is there any way where I can plot just zero for all the values beyond -0.6 an 0.6.但是有什么方法可以让 plot 对于所有超出 -0.6 和 0.6 的值都为零。 In short I want to increase range of my plot to make it consistent.简而言之,我想增加 plot 的范围以使其保持一致。

在此处输入图像描述

So far I am using this code:到目前为止,我正在使用此代码:

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()

My solution is based on passing ind parameter to plot .我的解决方案基于将ind参数传递给plot It specifies evaluation points for the estimated PDF.它指定了估计的 PDF 的评估点。 As the number of points I chose 700 but you can change it as you wish, eg to get more smooth curves.我选择了700的点数,但您可以根据需要更改它,例如获得更平滑的曲线。 For consistency, pass just the same border values to plt.xlim(...) .为了保持一致性,将相同的边框值传递给plt.xlim(...)

So change respective lines of your code to:因此,将代码的相应行更改为:

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)

Other possible correction is that instead of explicit looping over columns of your DataFrame, you can call your plot for the whole DataFrame:其他可能的更正是,您可以为整个DataFrame 调用 plot,而不是显式循环遍历 DataFrame 的列:

data_Pre1.plot.density(ind=np.linspace(minX, maxX, 700))

Edit编辑

The evaluation points specified with ind need not be evenly spaced throughout the whole x axis "wanted" range.ind指定的评估点不需要在整个x轴“需要”范围内均匀分布。

If you are sure about both "limits" of x axis "discovered" by the plotting function (you wrote -0.6 and 0.6 ), you can generate ind as densely spaced points only in this range and then:如果您确定通过绘图 function (您编写了-0.60.6 )“发现”了x轴的两个“限制”,则可以在此范围内将ind生成为密集间隔的点,然后:

  • prepend it with a single point - your "wanted" lower x limit,在它前面加上一个点 - 你的“想要的”下x限制,
  • append it with also a single point - your "wanted" upper x limit. append 它还有一个点 - 你的“想要的”上限x限制。

So you can change your code to:因此,您可以将代码更改为:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何 plot 和 pandas dataframe 并在 x 轴上进行分组? - How can I plot a pandas dataframe and have groupings in the x-axis? 如何将 x 轴设置为散景图上的日期时间? - How can I set the x-axis as datetimes on a bokeh plot? 如何更改 seaborn 小提琴 plot 的 x 轴标签而不返回并更改数据? - How can I change the labels of the x-axis of a seaborn violin plot without going back and changing the data? 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? Matplotlip plot 分组数据条形图增加x轴空间 - Matplotlip plot barchart of grouped data increase space of x-axis 如何指定要在x轴上绘制的离散值(matplotlib,boxplot)? - How can I specify the discrete values that I want to plot on the x-axis (matplotlib, boxplot)? Python Matplotlib-如何在X轴上绘制一条线? - Python matplotlib - How do I plot a line on the x-axis? 如何使用 matplotlib 中的日期时间更改 x 轴的范围? - How do I change the range of the x-axis with datetimes in matplotlib? 如何增加框 plot 的 x 轴上的值之间的空间? - How to increase space between values on x-axis of box plot? 如何正确排列我的数据和 plot x 轴? - How do I arrange my data and plot x-axis correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM