断开的 xx 轴和直方图中对数正态分布的调整

问题描述

我第一次做 x 轴断了的直方图,但我遇到了这些问题:

  1. 我无法让两个子图的条形和 xx 轴范围的宽度相同。右侧的子图似乎具有更大的比例。
  2. 我无法同时为两个子图放置 xx 轴的标签。我只想有一个居中的标签
  3. 两个子图的一些数量是叠加的,并且在两个子图连接的区域中有小的水平线(我无法删除这些小线)
  4. 一个子图中,xx 轴用逗号显示,另一个用点显示。我希望都是逗号。
  5. 我无法调整对数正态分布并计算该分布的均值和标准差。

我想了解如何制作此图表,因为之后我还有其他类似的图表要制作并可以练习,但现在一开始我有点迷茫,我真的需要你的帮助。您如何通过确定均值和标准差来解决之前的问题并调整对数正态分布?

Python 代码(Jupyter 笔记本 - Anaconda)

#amostra 17B (menor intervalo)
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
import matplotlib.ticker as tkr
import scipy,pylab
import locale
locale.setlocale(locale.LC_NUMERIC,"de_DE")
plt.rcParams['axes.formatter.use_locale'] = True

frequencia_relativa=[0.000,0.000,0.038,0.097,0.091,0.118,0.070,0.124,0.059,0.048,0.054,0.043,0.032,0.005,0.027,0.016,0.000]
x=[0.10,0.20,0.30,0.40,0.50,0.60,0.70,0.80,0.90,1.00,1.10,1.20,1.30,1.40,1.50,1.60,1.70,1.80,1.90,2.00,2.10,2.20,2.30,2.40,2.50,2.60,2.70,2.80,2.90,3.00,3.10,3.20,3.30,3.40,3.50,3.60,3.70,3.80,3.90,4.00,4.10,4.20,4.30,4.40,4.50,4.60,4.70,4.80,4.90,5.00,5.10,5.20,5.30,5.40,5.50,5.60,5.70,5.80,5.90,6.00,6.10,6.20,6.30,6.40,6.50,6.60,6.70,6.80,6.90,7.00,7.10,7.20,7.30,7.40,7.50,7.60,7.70,7.80,7.90,8.00]
plt.rcParams["figure.figsize"] = [20,8]
ax.yaxis.tick_left()
ax.tick_params(labeltop='off') # don't put tick labels at the top
ax2.yaxis.tick_right()
#fig,ax =plt.subplots()
f,(ax,ax2) = plt.subplots(1,2,sharey=True,facecolor='w')
ax.bar(x,height=frequencia_relativa,alpha=0.5,width=0.1,align='edge',edgecolor='black')
ax2.bar(x,edgecolor='black')
ax.tick_params(axis = 'both',which = 'major',labelsize = 18)
ax.tick_params(axis = 'both',which = 'minor',labelsize = 18)
ax2.tick_params(axis = 'both',labelsize = 18)
ax2.xaxis.set_ticks(np.arange(7.0,8.5,0.5))
ax2.xaxis.set_major_formatter(tkr.FormatStrFormatter('%0.1f'))
plt.subplots_adjust(wspace=0.04)
ax.set_xlim(0,3.0)
ax2.set_xlim(7.0,8.0)
# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
# This looks pretty good,and was fairly painless,but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to kNow here is that in axes coordinates,which are always
# between 0-1,spine endpoints are at these locations (0,0),(0,1),# (1,and (1,1).  Thus,we just need to put the diagonals in the
# appropriate corners of each of our axes,and so long as we use the
# right transform and disable clipping.

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot,just so we don't keep repeating them
kwargs = dict(transform=ax.transAxes,color='k',clip_on=False)
ax.plot((1-d,1+d),(-d,+d),**kwargs)
ax.plot((1-d,(1-d,**kwargs)

kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((-d,**kwargs)
ax2.plot((-d,**kwargs)

ax.set_xlabel('Tamanho lateral do triângulo ($\mu m$)',fontsize=22)
ax.set_ylabel('Frequência relativa',fontsize=22)
#x_axis = np.arange(0,29,0.001)
#ax.plot(x_axis,norm.pdf(x_axis,2.232,1.888),linewidth=3)
plt.show()
plt.savefig('output.png',dpi=500,bBox_inches='tight')

代码得到的图形:

enter image description here

图形类似于我想要的(但我希望我的轴断了):

enter image description here


更新代码和情节:

import numpy as np
from scipy.stats import lognorm
import matplotlib.ticker as tkr
import scipy,pylab
import locale
import matplotlib.gridspec as gridspec
from scipy.stats import lognorm
locale.setlocale(locale.LC_NUMERIC,8]
f,sharex=True,facecolor='w')
axes = f.add_subplot(111,frameon=False)
ax.plot(x,lognorm.pdf(x,0.903,0.713),linewidth=3)
ax.spines['top'].set_color('none')
ax2.spines['top'].set_color('none')
gs = gridspec.GridSpec(1,width_ratios=[3,1])
ax = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax.yaxis.tick_left()
ax.tick_params(labeltop='off') # don't put tick labels at the top
ax2.yaxis.tick_right()
ax.bar(x,8.0)
def func(x,pos):  # formatter function takes tick label and tick position
    s = str(x)
    ind = s.index('.')
    return s[:ind] + ',' + s[ind+1:]   # change dot to comma
x_format = tkr.FuncFormatter(func)
ax.xaxis.set_major_formatter(x_format)
ax2.xaxis.set_major_formatter(x_format)
# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
# This looks pretty good,clip_on=False)
ax.plot((1-d/3,1+d/3),**kwargs)
ax.plot((1-d/3,**kwargs)
ax2.tick_params(labelright=False)
ax.tick_params(labeltop=False)

f.text(0.5,-0.04,'Tamanho lateral do triângulo ($\mu m$)',ha='center',fontsize=22)
f.text(-0.02,0.5,'Frequência relativa',va='center',rotation='vertical',fontsize=22)
#ax.set_xlabel('Tamanho lateral do triângulo ($\mu m$)',fontsize=22)
#ax.set_ylabel('Frequência relativa',linewidth=3)
f.tight_layout()
plt.show()
#plt.savefig('output.png',bBox_inches='tight')

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...