为了账号安全,请及时绑定邮箱和手机立即绑定

Pyplot:以自定义比例绘制图形(x 和 y)

Pyplot:以自定义比例绘制图形(x 和 y)

catspeake 2023-03-22 10:50:01
我一直在 Jupyter Notebook 中使用以下代码绘制数据框:为了与仅在纸上以 0.005mm=1cm 比例提供的旧数据进行比较,我需要以相同比例导出和打印图形:0.005mm in the图(x 轴和 y 轴)在图中必须为 1cm。有什么办法可以定义自定义比例吗?有关信息,x 范围和 y 范围不是固定的,它们会根据我加载到数据框中的数据而有所不同。import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns            import matplotlib.ticker as tickerdf = pd.DataFrame(np.array([[1.7, 0], [1.75, -0.012], [1.8, 0]]),                   columns=['pos', 'val'])                  # Plot resultssns.set()plt.figure(figsize=(20,30))plt.plot(df['pos'], df['val'])ax = plt.axes()ax.set_aspect('equal')plt.xlabel('Position [mm]')plt.ylabel('Höhe [mm]')ax.xaxis.set_major_locator(ticker.MultipleLocator(0.005))ax.yaxis.set_major_locator(ticker.MultipleLocator(0.005))plt.show()
查看完整描述

2 回答

?
开满天机

TA贡献1786条经验 获得超12个赞


//img4.sycdn.imooc.com/641a6d060001aede06440259.jpg

import matplotlib.pyplot as plt


from mpl_toolkits.axes_grid1 import Divider, Size

from mpl_toolkits.axes_grid1.mpl_axes import Axes


cm = lambda d: d/2.54


x, y = [1700.0, 1725.0, 1750.0], [0.0, -12.0, 0.0] # μm

dx, dy = 50.0, 12.0


# take margins into account

xmin, xmax = min(x)-dx*0.05, max(x)+dx*0.05

ymin, ymax = min(y)-dy*0.05, max(y)+dy*0.05

dx, dy = xmax-xmin, ymax-ymin


# 5 μm data == 1 cm plot

scale = 5/1

xlen, ylen = dx/scale, dy/scale


# Now we know the extents of our data and the axes dimension,

# so we can set the Figure dimensions, taking borders into account

left, right = 2, 1

bot, top = 1.5, 1.5

fig = plt.figure(

    figsize=(cm(left+xlen+right), cm(bot+ylen+top)),

    dpi=118)


# change bg color to show so that one can measure the figure

# and the axes when pasted into SO and do their math…

fig.set_facecolor('xkcd:grey teal')


##########  Below is stolen from Matplotlib Fixed Size Axes

########## (please don't ask me…)

# Origin and size of the x axis and y axis

h = [Size.Fixed(cm(left)), Size.Fixed(cm(xlen))]

v = [Size.Fixed(cm(bot)), Size.Fixed(cm(ylen))]

divider = Divider(fig, (0.0, 0.0, 1., 1.), h, v, aspect=False)

# NB: Axes is from mpl_toolkits.axes_grid1.mpl_axes

ax = Axes(fig, divider.get_position())

ax.set_axes_locator(divider.new_locator(nx=1, ny=1))

fig.add_axes(ax)

#########  Above is stolen from Matplotlib Fixed Size Axes Demo 


plt.plot(x,y)

plt.grid()

ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax), yticks=range(-12,1,3),

       xlabel='X/μm', ylabel='Y/μm',

       title='X vs Y, 1 cm on plot equals 5 μm')

fig.suptitle('Figure dimensions: w = %.2f cm, h = %.2f cm.'%(

    left+xlen+right, bot+ylen+top))


fig.savefig('Figure_1.png',

            # https://stackoverflow.com/a/4805178/2749397, Joe Kington's

            facecolor=fig.get_facecolor(), edgecolor='none')


查看完整回答
反对 回复 2023-03-22
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

1 英寸 = 2.54 厘米,所以 254/0.005 = 50800 dpi

plt.figure(figsize=(20,30), dpi=50800)


查看完整回答
反对 回复 2023-03-22
  • 2 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信