我正在寻找一种描述性地分散pandas.DataFrame与此类似的方法:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 type 1000 non-null object
1 value 1000 non-null int64
2 count 1000 non-null int64
dtypes: int64(2), object(1)
memory usage: 23.6+ KB
Run Code Online (Sandbox Code Playgroud)
使用pandas.DataFrame.plot或seaborn.scatterplot,每个点type都放置在一条彼此重叠的垂直线上。为了缓解这个问题,我想在 x 方向至少引入一些抖动,但我不知道如何实现。
到目前为止我的情节:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 …Run Code Online (Sandbox Code Playgroud) import plotly.graph_objects as go
import numpy as np
import numpy
# Download data set from plotly repo
pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
x, y, z = pts.T
US = [(1970, 4688.5), (1971, 4559.4), (1972, 4807.0), (1973, 4994.6), (1974, 4827.4), (1975, 4612.5), (1976, 4907.3),
(1977, 5071.4), (1978, 5066.2), (1979, 5105.1), (1980, 4877.4), (1981, 4808.0), (1982, 4562.2), (1983, 4541.5),
(1984, 4751.8), (1985, 4777.2), (1986, 4736.6), (1987, 4893.1), (1988, 5114.1), (1989, 5171.4), (1990, 5063.9),
(1991, 5022.3), (1992, 5103.5), (1993, 5222.5), (1994, 5305.7), (1995, 5357.6), (1996, …Run Code Online (Sandbox Code Playgroud) 我是新来的,仍在学习 R,所以我正在处理一个错误。
这是我从控制台得到的信息
不知道如何自动选择 Haven_labelled/vctrs_vctr/double 类型的对象的比例。默认为连续。
我不知道我能做些什么来让它发挥作用。我想得到一个散点图。
ggplot(data = diagnoza, aes(x = Plecc, y = P32.01))
Run Code Online (Sandbox Code Playgroud)
不知道如何自动选择 Haven_labelled/vctrs_vctr/double 类型的对象的比例。默认为连续。
我对 sns 线图和散点图有问题。基本上我想做的是连接散点图的点以呈现连接映射点的最近线。当面对具有相同 x 轴值的点时,线图会以某种方式改变宽度。我希望线图始终保持相同的实线。
代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
data = {'X': [13, 13, 13, 12, 11], 'Y':[14, 11, 13, 15, 20], 'NumberOfPlanets':[2, 5, 2, 1, 2]}
cts = pd.DataFrame(data=data)
plt.figure(figsize=(10,10))
sns.scatterplot(data=cts, x='X', y='Y', size='NumberOfPlanets', sizes=(50,500), legend=False)
sns.lineplot(data=cts, x='X', y='Y',estimator='max', color='red')
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果:
有任何想法吗?
编辑:
如果我尝试使用 pyplot 它也不起作用:代码:
plt.plot(cts['X'], cts['Y'])
Run Code Online (Sandbox Code Playgroud)
结果:
我需要一条线,它连接最近的点(基本上是图像一上显示的,但具有相同的实线)。
我正在寻找一种方法,以响应线条角度的方式为 matplotlib 中的线条图指定颜色。这是我当前的代码:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
horz = [[0.5,0.6,0.8],[0.1,0.8,0.9],[0.2,0.5,0.9]]
vert = [[0.1,0.2,0.3],[0.05,0.1,0.15],[0.2,0.3,0.35]]
f = plt.figure(figsize=(6,6))
ax = plt.axes()
for column in range(0,len(horz)):
x = np.array(horz[column])
y = np.array(vert[column])
#LINEAR TRENDLINE
z = np.polyfit(horz[column], vert[column], 1)
p = np.poly1d(z)
ax.plot(horz[column],p(horz[column]),"-")
plt.arrow(x=horz[column][-2],y=p(horz[column])[-2],dx=(horz[column][-1]-horz[column][-2]),dy=(p(horz[column])[-1]-p(horz[column])[-2]), shape='full', lw=.01,
length_includes_head=True, head_width=.012, head_length=0.02, head_starts_at_zero=False, overhang = 0.5)
#FIG SETTINGS
plt.xlim([0, 1])
plt.ylim([0.1,0.5])
ax.set_title('Title',
fontsize = 14)
Run Code Online (Sandbox Code Playgroud)
这里的想法是,如果线的角度为 0 度,则它将位于给定梯度的一端,如果线的角度为 90 度,则位于另一端。此外,我希望将线条长度视为颜色的强度。因此,如果线条短,它会更接近白色,如果线条长,它会更接近渐变的原始颜色。
下面的代码创建了一个带有白点的散点图.如何在不重绘整个图形的情况下删除此点?
g = Figure(figsize=(5,4), dpi=60);
b = g.add_subplot(111)
b.plot(x,y,'bo') # creates a blue dot
b.plot(x,y,'wo') # ovverrides the blue dot with a white dot (but the black circle around it remains)
Run Code Online (Sandbox Code Playgroud) 我有一些成对测量的数据(例如 1C,1M,2C和2M),我分别绘制了这些数据(如C和M)。但是,我想在每对之间添加一条线(例如,从C列的点1到M'列'的点1的线)。
整个数据集的一小部分:
PairNumber Type M
1 M 0.117133
2 M 0.054298837
3 M 0.039734
4 M 0.069247069
5 M 0.043053957
1 C 0.051086898
2 C 0.075519
3 C 0.065834198
4 C 0.084632915
5 C 0.054254946
Run Code Online (Sandbox Code Playgroud)
我使用以下微小的R代码段生成了以下图片:
boxplot(test$M ~ test$Type)
stripchart(test$M ~ test$Type, vertical = TRUE, method="jitter", add = TRUE, col = 'blue')
Run Code Online (Sandbox Code Playgroud)
我想知道实现此目标需要什么命令或功能(下面仅给出了一些代码,给出了所需结果的粗略草图)。
另外,用ggplot进行此操作对我也很好,我可以使用以下替代ggplot代码来生成类似于上述第一个图的图:
ggplot(,aes(x=test$Type, y=test$M)) +
geom_boxplot(outlier.shape=NA) +
geom_jitter(position=position_jitter(width=.1, height=0))
Run Code Online (Sandbox Code Playgroud)
我一直在尝试geom_path,但是我没有找到实现我想要的正确语法。
我有一个7列(2因子,5位数)的数据框(“数据”)。第一列包含7个不同国家的名称,在接下来的列中,我收集了表征每个国家的不同参数(例如人口,GDP等)的数据。在最后一列中,因子变量指定相应国家/地区所属的大陆。
数据如下所示:
structure(list(Country = structure(c(5L, 4L, 7L, 2L, 1L, 6L,
3L), .Label = c("Brazil", "Chile", "China", "France", "Germany",
"India", "Netherlands"), class = "factor"), GDP = c(0.46, 0.57,
0.75, 0.56, 0.28, 0.88, 1), Population = c(0.18, 0.09, 0.54,
0.01, 0.02, 0.17, 0.84), Birth.rate = c(87.21, 18.34, 63.91,
14.21, 5.38, 51.19, 209.26), Income = c(43.89, 18.23, 63.91,
12.3, 0.1, 14.61, 160.82), Savings = c(43.32, 0.11, 0, 1.91,
5.29, 36.58, 50.38), Continent = structure(c(2L, 2L, 2L, 3L,
3L, 1L, 1L), .Label = c("Asia", …Run Code Online (Sandbox Code Playgroud) import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set(style="darkgrid")
g = sns.scatterplot(x="Area", y="Rent/Sqft", hue="region", style="availability", data=df)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下图。
我想将图例移出情节。我用谷歌搜索并尝试了以下
g.legend(loc='right', bbox_to_anchor=(1.25, 0.5), ncol=1)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是我没有任何输出。而且,我无法理解对象plt如何连接到我的sns对象
我正在使用Jupyter Notebook,Python 3.6和Seaborn 0.9.0。