GVKun编程网logo

Python-matplotlib / seaborn:将第一行和最后一行切成一半的热图图(python如何把一行切成多行)

13

对于Python-matplotlib/seaborn:将第一行和最后一行切成一半的热图图感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python如何把一行切成多行,并为您提供关于java–

对于Python-matplotlib / seaborn:将第一行和最后一行切成一半的热图图感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python如何把一行切成多行,并为您提供关于java – 如何在reader.readLine()中检测第一行和最后一行?、Matplotlib Toolkits:python 高级绘图库 seaborn、matplotlib – 如何在Seaborn boxplot中加宽盒子?、php-选择除第一行和最后一行以外的所有行的有用信息。

本文目录一览:

Python-matplotlib / seaborn:将第一行和最后一行切成一半的热图图(python如何把一行切成多行)

Python-matplotlib / seaborn:将第一行和最后一行切成一半的热图图(python如何把一行切成多行)

如何解决Python-matplotlib / seaborn:将第一行和最后一行切成一半的热图图?

不幸的是,matplotlib 3.1.1 打破了海洋热图;并且一般来说,带有固定刻度的倒置轴。 在当前的开发版本中已修复此问题;你可能因此

  • 恢复到matplotlib 3.1.0
  • 使用matplotlib 3.1.2或更高版本
  • 手动设置热图限制(ax.set_ylim(…))

解决方法

当用seaborn(以及使用matplotlib的相关矩阵)绘制热图时,第一行和最后一行被切成两半。当我运行此在线最小代码示例时,也会发生这种情况。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.read_csv(''https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv'')
plt.figure(figsize=(10,5))
sns.heatmap(data.corr())
plt.show()

并获得此结果(目前还不允许我嵌入图像) y轴上的标签在正确的位置,但是行并不完全在此处。

几天前,它按预期工作。从那时起,我安装了texlive-xetex,因此我再次将其删除,但并不能解决我的问题。

有什么想法我可能会错过吗?

java – 如何在reader.readLine()中检测第一行和最后一行?

java – 如何在reader.readLine()中检测第一行和最后一行?

我以下面的方式读取文件的每一行
BufferedReader in = new BufferedReader(new FileReader(inFile));

while (null != (line = in.readLine())) {


}

我想在第一行和最后一行单独进行一些验证.有没有
检查它是否是while循环中的第一行和最后一行

while (null != (line = in.readLine())) {    

    if(firstlineoffile) {
    }

    else if (lastlineoffile) {
    }

    else
    {
    }

}

解决方法

酷问题我打了一圈,这是一个 SSCCE,只是copy’n’paste’n’run它.
package com.stackoverflow.q2292917;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class Test {

    public static void main(String... args) throws IOException {
        // Create test file.
        File file = new File("/test.txt");
        PrintWriter writer = new PrintWriter(file);
        writer.println("line 1");
        writer.println("line 2");
        writer.println("line 3");
        writer.println("line 4");
        writer.println("line 5");
        writer.close();

        // Read test file.
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String next,line = reader.readLine();
            for (boolean first = true,last = (line == null); !last; first = false,line = next) {
                last = ((next = reader.readLine()) == null);

                if (first) {
                    System.out.println("first line: " + line);
                } else if (last) {
                    System.out.println("last line: " + line);
                } else {
                    System.out.println("normal line: " + line);
                }
            }
        } finally {
            if (reader != null) try { reader.close(); } catch (IOException logorIgnore) {}
        }

        // Delete test file.
        file.delete();
    }

}

输出:

first line: line 1
normal line: line 2
normal line: line 3
normal line: line 4
last line: line 5

但是,我对初学者的可读性和可解释性提出质疑…;)

Matplotlib Toolkits:python 高级绘图库 seaborn

Matplotlib Toolkits:python 高级绘图库 seaborn

http://blog.csdn.net/pipisorry/article/details/49515745

Seaborn 介绍

seaborn

(Not distributed with matplotlib)

seaborn is a highlevel interface for drawing statistical graphics with matplotlib. Itaims to make visualization a central part of exploring andunderstanding complex datasets.

[seaborn ¶]

Matplotlib 是 Python 主要的绘图库。但是不建议你直接使用它,原因与不推荐你使用 NumPy 是一样的。虽然 Matplotlib 很强大,它本身就很复杂,你的图经过大量的调整才能变精致。因此,作为替代推荐一开始使用 Seaborn。

Seaborn 本质上使用 Matplotlib 作为核心库(就像 Pandas 对 NumPy 一样)。

seaborn 的优点:

  1. 默认情况下就能创建赏心悦目的图表。(只有一点,默认不是 jet colormap)
  2. 创建具有统计意义的图
  3. 能理解 pandas 的 DataFrame 类型,所以它们一起可以很好地工作。

安装 pip install seaborn

Note: lz 发现,就算你不用 seaborn 绘图,只要在 matplotlib 绘图中加上 seaborn 的 import 语句,就会以 seaborn 的图形方式展示图片,具有 seaborn 的效果。如:

import seaborn
import matplotlib.pyplot as plt

注意要显示出图形,需要引入 matplotlib 并 plt.show () 出来。

皮皮 blog


Seaborn 使用

Style functions: API | Tutorial

Color palettes: API | Tutorial

Distribution plots: API | Tutorial

Regression plots: API | Tutorial

Categorical plots: API | Tutorial

Axis grid objects: API | Tutorial

分布图绘制 Distribution plots

jointplot(x, y[, data, kind, stat_func, ...]) Draw a plot of two variables with bivariate and univariate graphs.
pairplot(data[, hue, hue_order, palette, ...]) Plot pairwise relationships in a dataset.
distplot(a[, bins, hist, kde, rug, fit, ...]) Flexibly plot a univariate distribution of observations.
kdeplot(data[, data2, shade, vertical, ...]) Fit and plot a univariate or bivariate kernel density estimate.
rugplot(a[, height, axis, ax]) Plot datapoints in an array as sticks on an axis.

单变量绘制 distplot

seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)

Note:

1 如果想显示统计个数而不是概率,需要同时设置 norm_hist=False, kde=False。

2 自己指定 fit 的函数 from scipy import stats ... fit=stats.norm

>>> import seaborn as sns, numpy as np
>>> sns.set(rc={"figure.figsize": (8, 4)}); np.random.seed(0)
>>> x = np.random.randn(100)
>>> ax = sns.distplot(x)

双变量 + 单变量统一绘制 jointplot

import seaborn as sns
 
# Load one of the data sets that come with seaborn
tips = sns . load_dataset ( "tips" )
 
sns . jointplot ( "total_bill" , "tip" , tips , kind = ''reg'' ) ;
 

如你所见,仅通过一行代码,我们就创建了一个漂亮复杂的统计图,其中包含拥有置信区间的最拟合回归直线、边界图,以及相关系数。
使用 matplotlib 重新绘制这幅图的话需要相当多的(丑陋)代码,包括调用 scipy 执行线性回归并手动利用线性回归方程绘制直线(我甚至想不出怎么在边界绘图,怎么计算置信区间)。
[ the tutorial on quantitative linear models]

与 Pandas 的 DataFrame 很好地工作

数据有自己的结构。通常我们感兴趣的包含不同的组或类(这种情况下使用 pandas 中 groupby 的功能会让人感到很神奇)。比如 tips(小费)的数据集是这样的:

tips . head ( )

Out[9]:

  total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

我们可能想知道吸烟者给的小费是否与不吸烟的人不同。没有 seaborn 的话,这需要使用 pandas 的 groupby 功能,并通过复杂的代码绘制线性回归直线。使用 seaborn 的话,我们可以给 col 参数提供列名,按我们的需要划分数据:

回归图绘制 Regression plots

lmplot(x, y, data[, hue, col, row, palette, ...]) Plot data and regression model fits across a FacetGrid.
regplot(x, y[, data, x_estimator, x_bins, ...]) Plot data and a linear regression model fit.
residplot(x, y[, data, lowess, x_partial, ...]) Plot the residuals of a linear regression.
interactplot(x1, x2, y[, data, filled, ...]) Visualize a continuous two-way interaction with a contour plot.
coefplot(formula, data[, groupby, ...]) Plot the coefficients from a linear model.

sns . lmplot ( "total_bill" , "tip" , tips , col = "smoker" ) ;
 

很整洁吧?随着你研究得越深,你可能想更细粒度地控制这些图表的细节。因为 seaborn 只是调用了 matplotlib,那时你可能会想学习这个库。

from: http://blog.csdn.net/pipisorry/article/details/49515745

ref: [seaborn API reference]*

[Seaborn tutorial]*

Python 和数据科学的起步指南

Example gallery

Python 数据可视化模块:Seaborn


matplotlib – 如何在Seaborn boxplot中加宽盒子?

matplotlib – 如何在Seaborn boxplot中加宽盒子?

我正在尝试使用Seaborn( Reference)制作一个分组的盒子图,盒子都非常窄 – 太窄而无法看到分组颜色.

g = seaborn.factorplot("project_code",y="num_mutations",hue="organ",data=grouped_donor,kind="Box",aspect=3)

如果我放大或者将图形拉伸几倍于我的屏幕宽度,我可以看到这些框,但显然这不是一个标准图形.

这似乎是我的数据量的函数;如果我只绘制前500点(6000点),我会得到可见但小的盒子.它可能特别是我的数据的高差异的函数;根据matplotlib Boxplot文档,

The default [width] is 0.5,or 0.15x(distance between extreme positions) if that is smaller.

无论是什么原因,如果我可以加宽它们,那么图表本身就有足够的空间用于更宽的盒子.

不幸的是,控制框宽度的Boxplot关键字宽度不是有效的factorplot关键字,我找不到matplotlib函数,它会改变绘图函数本身之外的条形图或框的宽度.我甚至找不到任何人讨论这件事;我找到的最接近的是Boxplot线宽.有什么建议?

解决方法

为了将来的参考,以下是使用图例制作正确图形的相关代码位:(显然这是缺少重要的东西,实际上不会按原样运行,但希望它显示了棘手的部分)

import matplotlib.pylab as pyp
import seaborn as sns

def custom_legend(colors,labels,legend_location = 'upper left',legend_boundary = (1,1)):
    # Create custom legend for colors
    recs = []
    for i in range(0,len(colors)):
        recs.append(mpatches.Rectangle((0,0),1,fc=colors[i]))
    pyp.legend(recs,loc=legend_location,bBox_to_anchor=legend_boundary)

# Color Boxplots by organ
organ_list = sorted(df_unique(grouped_samples,'type'))
colors = sns.color_palette("Paired",len(organ_list))
color_dict = dict(zip(organ_list,colors))
organ_palette = grouped_samples.drop_duplicates('id')['type'].map(color_dict)

# Plot grouped Boxplot
g = sns.factorplot("id","num_mutations",data=grouped_samples,order=id_list,size=7,aspect=3,palette=organ_palette)
sns.despine(left=True)
plot_setup_pre()
pyp.yscale('log')
custom_legend(colors,organ_list)

php-选择除第一行和最后一行以外的所有行

php-选择除第一行和最后一行以外的所有行

我希望数据库显示除第一行和最后一行之外的所有行,因为我有针对它们的CSS代码.

我尝试了这个:

SELECT * FROM db 
WHERE 
keywords LIKE '%example%' LIMIT 9999 OFFSET 1
 AND 
keywords LIKE '%example%' DESC LIMIT 9999 OFFSET 1

由于行号可能会增加,因此我无法输入确切的数字.

解决方法:

确实没有理由通过尝试在sql级别截断这些值来使查询复杂化,您可以执行以下操作:

$results = $db->query( /* regular query with all results */ );

array_pop($results);
array_shift($results);

// Now use $results, which Now contains only the "middle" content

如果您确实希望在数据库级别上使用它,则可以使用:

SELECT * FROM db 
WHERE keywords LIKE '%example%' 
AND id <> (SELECT MAX(ID) FROM TABLE)
AND id <> (SELECT MIN(ID) FROM TABLE)

关于Python-matplotlib / seaborn:将第一行和最后一行切成一半的热图图python如何把一行切成多行的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于java – 如何在reader.readLine()中检测第一行和最后一行?、Matplotlib Toolkits:python 高级绘图库 seaborn、matplotlib – 如何在Seaborn boxplot中加宽盒子?、php-选择除第一行和最后一行以外的所有行等相关内容,可以在本站寻找。

本文标签: