相关疑难解决方法(0)

使用twiny时,Python Matplotlib图标题与轴标签重叠

我试图使用twiny在同一图表上绘制两个单独的数量,如下所示:

fig = figure()
ax = fig.add_subplot(111)
ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-')
ax.set_yscale('log')
ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000))
ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0]))
ax.set_xlabel('Rotational period (hrs)')
ax.set_ylabel('Orbital radius (km), logarithmic')
ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top')


ax2 = ax.twiny()
ax2.plot(v,r,'k-')
ax2.set_xlabel('Linear speed (ms-1)')

show()
Run Code Online (Sandbox Code Playgroud)

并且数据显示得很好,但我遇到的问题是图形标题与辅助x轴上的轴标签重叠,因此它几乎不易读(我想在这里发布图片示例,但我没有足够高的代表).

我想知道是否有一种简单的方法可以直接将标题移动几十个像素,这样图表看起来更漂亮.

python matplotlib title figure

128
推荐指数
6
解决办法
15万
查看次数

Matplotlib - 标记每个bin

我目前正在使用Matplotlib来创建直方图:

在此输入图像描述

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as pyplot
...
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1,)
n, bins, patches = ax.hist(measurements, bins=50, range=(graph_minimum, graph_maximum), histtype='bar')

#ax.set_xticklabels([n], rotation='vertical')

for patch in patches:
    patch.set_facecolor('r')

pyplot.title('Spam and Ham')
pyplot.xlabel('Time (in seconds)')
pyplot.ylabel('Bits of Ham')
pyplot.savefig(output_filename)
Run Code Online (Sandbox Code Playgroud)

我想让x轴标签更有意义.

首先,这里的x轴刻度似乎限于五个刻度.无论我做什么,我似乎无法改变这一点 - 即使我添加更多xticklabels,它只使用前五个.我不确定Matplotlib如何计算这个,但我认为它是从范围/数据中自动计算的?

有没有什么办法可以提高x-tick标签的分辨率 - 甚至可以提高每个条形码/ bin 的分辨率

(理想情况下,我也希望以微秒/毫秒重新格式化秒数,但这是另一天的问题).

其次,我想要标记每个单独的条形图 - 包含该条形图中的实际数字,以及所有条形图总数的百分比.

最终输出可能如下所示:

在此输入图像描述

Matplotlib有可能吗?

干杯,维克多

python graphing visualization matplotlib histogram

64
推荐指数
1
解决办法
8万
查看次数

Matplotlib:图形左边缘和 y 轴之间的固定间距

我用 Python 2.7 在 Matplotlib 中生成了 2 个图。绘图保存为 *.png 文件。保存后,两个图像具有相同的分辨率 - 宽度 = 1099 像素,高度 = 619 像素。

然而,当我垂直对齐保存的 *.png 图像时(见下图),y 轴和图像最左边点之间的间距并不相同 - 请参见下图中的ab 。在此输入图像描述

我的意思是,从图像左侧到 y 轴的距离不一样(a 不等于 b)。

单击图像放大并查看。

问题: 有没有办法强制 y 轴从相对于图像左侧的特定位置开始?

注意:我不关心刻度标签和轴标签之间的空间 - 我可以使用类似的东西来调整它ax.yaxis.labelpad(25)。但是,我不知道如何修复图像左侧和 y 轴之间的空间。

注 2:我使用以下方法创建绘图:

fig = plt.figure(1)
ax = fig.add_subplot(111)
fig.tight_layout()
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib python-2.7

7
推荐指数
1
解决办法
7249
查看次数