- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 matplotlib 为我的论文创建图表。我正在使用以下代码:
import numpy as np
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
fig_width_pt = 390 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 10, # LaTeX default is 10pt font.
"text.fontsize": 10,
"legend.fontsize": 8, # Make the legend/label fonts a little smaller
"xtick.labelsize": 8,
"ytick.labelsize": 8,
"figure.figsize": fig_size,
'axes.linewidth': .5,
'lines.linewidth': .5,
'patch.linewidth': .5,
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
]
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import colorsys
def savefig(filename):
plt.savefig('{}.pgf'.format(filename))
plt.savefig('{}.pdf'.format(filename))
# setup
title = 'Clustering after salting out'
ylabel = '% of total colloids'
xlabel = 'Cluster size'
xticklabels = ('1','2','3','4','5','6','7','5+')
legend = ['10min', '20min','30min']
# read data from files
# skipped this part for legibility
# calculations with data, skipped for legibility
# plot it in a bar plot
N = len(ys[0])
ind = np.arange(0, N+1, 1.2) # the x locations for the groups
width = 0.35 # the width of the bars
# generate colours
hsv_colours = [(x*1.0/N, 0.8, 0.8) for x in range(N)]
rgb_colours = map(lambda x: colorsys.hsv_to_rgb(*x), hsv_colours)
fig, ax = plt.subplots()
rects = [ax.bar([x+i*width for x in ind], y, width, color=rgb_colours[i], yerr=errors_percentage[i]) for i,y in enumerate(ys)]
# add some info
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
ax.set_title(title)
ax.set_xticks(ind+width)
ax.set_xticklabels(xticklabels)
ax.axis([0,7,0,60])
ax.legend(rects, legend)
savefig('tpm_cluster_statistics')
生成的输出如下所示:
如您所见,条形图的最后一个条没有完全填满。我需要一些其他设置才能使其正常工作吗?目标是创建一个 PGF 文件以包含在 LaTex 文档中。 PDF 文件仅供预览。部分填充的条在 PDF 和 PGF 文件中都有。非常感谢任何帮助!
编辑:回复 tcaswell:这是您可以在计算机上尝试的最小工作示例:
import numpy as np
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import colorsys
def savefig(filename):
plt.savefig('{}.pgf'.format(filename))
plt.savefig('{}.pdf'.format(filename))
# setup
title = 'Title'
ylabel = 'x'
xlabel = 'y'
xticklabels = ('1','2','3','4','5','6','7','8')
legend = ['1', '2','3']
#data
ys = [[51.63593099345628, 28.911362284354553, 12.135633551457465, 4.521118381915526, 1.189767995240928, 0.7138607971445567, 0.41641879833432477, 0.4759071980963712], [46.66359871145882, 21.445006902899216, 14.496088357109988, 7.363092498849516, 4.1417395306028535, 3.313391624482283, 0.0, 2.577082374597331], [52.642595499738356, 22.39665096807954, 12.087912087912088, 7.744636316064887, 2.3547880690737837, 1.5698587127158554, 0.3663003663003663, 0.837257980115123]]
# plot it in a bar plot
N = len(ys[0])
ind = np.arange(0, N+1, 1.2) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects = [ax.bar([x+i*width for x in ind], y, width) for i,y in enumerate(ys)]
# add some info
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
ax.set_title(title)
ax.set_xticks(ind+width)
ax.set_xticklabels(xticklabels)
ax.axis([0,7,0,60])
ax.legend(rects, legend)
savefig('tpm_cluster_statistics')
然后结果是这样的:
但是当我删除这些行时:
mpl.use('pgf')
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
}
mpl.rcParams.update(pgf_with_latex)
只需使用 plt.show()
显示输出,结果看起来确实正确。
最佳答案
我不知道是什么真正导致了它,但是在没有 mpl.use('pgf')
的情况下切换到仅 PDF 输出解决了这个问题。现在我会坚持使用 PDF,这也很好。感谢所有的帮助!
关于python - matplotlib barplot 的最后一个栏被填充了一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23552636/
简单的barplot,其值位于条形顶部(我知道这很愚蠢 - 我被迫添加它们:))。 text 效果很好,但最高频率条上方的值被隐藏。我尝试了边距,但它移动了整个图,而不仅仅是图形区域。你有什么建议?谢
我有一个数据框,其中一个变量(我们称之为 Q1)有几个级别:“无用”、“30 分钟”、“1 小时”、“2 小时”、“3+ 小时”。 如何绘制 barplot(),其中条形按因子水平的顺序排列?我尝试使
我试图在图中展示较差的设计选择。人们使用的一种浪费墨水、可能分散注意力的效果是条形的阴影效果。我想让ggplot2做到这一点。我的基本想法是使第一个半透明的条形层稍高并向右移动。我可以得到稍高一点的位
我在 R 中有一个数据框。每个数据框内有 8 个变量。 我想制作一个条形图,其中我想根据一个特定变量中的值为条形分配不同的灰色阴影。 这就是我制作条形图的方式: barplot(x$cov, name
我尝试创建一个条形图,它同时使用和堆叠。我有一些类似于我想要的方面的东西: tmp <- morley tmp$loc <- paste("No", tmp$Run %/% 2, sep="") tm
我有一个 Pandas 数据框,其中有几个组列,如下所示。 gr1 grp2 variables lb m ub A A1 V1 1.00 1.50
我已将 CSV 文件导入到数据框中并创建了数据透视表: Amount Date 2018-09-30 222 2018-10-01 1
我有以下数据集: words = ['upvoted', 'upvote', 'f***', 'reimer', 'feminists', 'censorship', 'wet', '0
我以前从未见过这个,但是我正在制作的 seaborn barplot 不会均匀地间隔条形...... 代码 fig, ax = plt.subplots(figsize=(25,6)) sns.bar
我有一个条形图,在 X 轴上有类别并在 Y 上计数。有没有办法按 Y 值的降序对条形图进行排序? 这是示例代码 Animals <- c("giraffes", "orangutans", "monk
我正在 ggplot2 中制作一个躲避条形图,一个分组的计数为零,我想显示。我记得在 HERE 上看到过这个前阵子想通了 scale_x_discrete(drop=F)会工作。它似乎不适用于躲避条。
我在使用 Dimple.JS 时遇到问题,他们的分组/顺序规则方法不允许我按照我的意愿控制条形列的顺序或对齐方式。 即使 x 轴顺序已关闭,我也必须在数据中添加名为“顺序”的行,以确保图表的 x 轴按
我想要实现的目标与此处已经提出的完全相同(特别是使用 R 的基本图形,而不是像 ggplot 或lattice 这样的包):Ordering bars in barplot() 但是,那里提出的解决方
我正在尝试实现这样的分布图 对于每个时期,我需要绘制“堆积比例”图表。 数据基本上是这样的: 400 401 402 403 404 ... 1013662 7 7 7
我正在使用 matplotlib 为我的论文创建图表。我正在使用以下代码: import numpy as np import numpy as np import matplotlib as mpl
我用 D3 画了一个条形图。我希望 x 轴值位于底部,y 值位于其本身的条形图上。 由于某种奇怪的原因,标签没有显示。 任何人都可以看一下this fiddle,我将不胜感激. 最后的相关部分 //
我有一本看起来像这样的字典 d = { 'a' : {'L1':2, 'L2':5, 'L3':8}, 'b' : {'L1':4, 'L2':7, 'L3':10}, 'c'
我正在尝试在 Matplotlib 中以对数刻度绘制条形图。如果我手动执行此操作(下图 3),我会得到正确的答案,但如果我使用 matplotlib 的 set_yscale('log') 或使用条形
我正在尝试按组显示相对百分比以及 sns 条形图中的总频率。我比较的两组在大小上有很大不同,这就是为什么我在下面的函数中按组显示百分比。 这是我创建的示例数据框的语法,它在目标分类变量(“项目”)中具
我在条形图中处理概率数据。条形图看起来不错,但是当我尝试添加错误栏时,它们没有组织或绑定(bind)到适当的列。如果我不必手动执行此操作,那就太完美了。 (此示例仅显示了一个子集。) 你能帮我弄清楚如
我是一名优秀的程序员,十分优秀!