如何制作一个箱线图,以月份为X轴,数据为Y轴。

huangapple go评论138阅读模式
英文:

How to make a boxplot in using month as x-axis and data as y-axis

问题

你可以在Python中使用matplotlib库的boxplot函数来实现相似的效果。以下是相应的代码:

import matplotlib.pyplot as plt

data = [delta_gmb_gris[date_month == i] for i in range(1, 13)]

fig, ax = plt.subplots(figsize=(10, 7))

# Creating boxplot
bp = ax.boxplot(data)

# Setting labels and title
ax.set_xticklabels([str(i) for i in range(1, 13)])
ax.set_xlabel('Month')
ax.set_ylabel('Data')
ax.set_title('Boxplot of Data by Month')

# Show plot
plt.show()

这段代码将按月份将数据分组,并创建一个相应的箱线图,x轴表示月份,y轴表示数据。

英文:

I have the following datasets (see below).

import numpy as np

date_month =\
np.array([ 4,  5,  8,  9, 10, 11, 12,  1,  2,  3,  4,  5,  7,  8,  9, 10, 11,
       12,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,  1,  2,  3,  4,
        5,  6,  7,  8,  9, 10, 11, 12,  1,  2,  3,  4,  5,  6,  7,  8,  9,
       10, 11, 12,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,  1,  2,
        3,  4,  5,  6,  7,  8,  9, 10, 11, 12,  1,  2,  3,  4,  5,  6,  7,
        8,  9, 10, 11, 12,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
        2,  3,  4,  5,  7,  8,  9, 10, 10, 12,  1,  2,  3,  4,  6,  7,  8,
        9, 11, 12,  1,  2,  4,  5,  6,  7, 10, 11, 12,  1,  3,  4,  5,  6,
        8,  9, 10, 11,  1,  2,  3,  4,  4,  7,  8,  9, 12,  1,  2,  3,  5,
        6,  7,  8, 11, 12,  1,  4,  4,  5,  6,  6,  7, 10, 11, 12,  1,  2,
        3,  4,  5,  6,  7,  8,  9, 10, 11, 12,  1,  2,  3,  4,  5,  6,  7,
        8,  9, 10, 11, 12,  1,  2])

delta_gmb_gris =\
np.array([ 0.00000000e+00,  6.52332764e+01, -3.21059875e+02, -1.16000977e+02,
        1.49104065e+02,  2.64947510e+00,  6.44912109e+01,  2.50497498e+02,
       -2.01494080e+02,  6.72978516e+01,  3.90867310e+01, -4.75610352e+00,
       -1.88961121e+02, -2.03859741e+02,  8.72711182e+00,  8.64827881e+01,
        3.07638550e+01, -6.92280273e+01,  1.55065308e+01,  6.16711426e+00,
        3.14987793e+01,  7.76036987e+01, -4.47878418e+01,  2.07130737e+01,
       -1.71567993e+02, -1.27824707e+02, -4.09106445e+01, -6.65779724e+01,
        1.22063568e+02,  1.01320801e+01, -7.25188599e+01,  5.64022827e+01,
        7.34244995e+01,  1.50355225e+01,  3.38818359e+01, -4.31621094e+01,
       -1.35590759e+02, -1.79813110e+02, -1.11535080e+02,  1.75334015e+01,
        3.81227112e+00,  1.05325272e+02, -9.73476105e+01,  3.31967773e+01,
        1.25141800e+02, -5.99645996e+01,  6.89667358e+01, -4.80169373e+01,
       -5.99559021e+01, -1.39211884e+02, -1.25912407e+02,  2.10015106e+01,
        1.06963715e+02, -3.59637375e+01,  2.01712189e+01,  7.62488937e+01,
       -5.14585114e+01,  1.07143402e+01,  3.43760834e+01, -2.37678070e+01,
       -1.97999802e+02, -1.57171906e+02, -3.81405640e+01, -2.07762146e+01,
        4.34190369e+01, -3.78499146e+01,  5.10930328e+01,  8.71820450e+01,
       -7.65830231e+00,  9.55678940e+01, -9.06094055e+01, -3.79481125e+01,
       -1.51231674e+02, -2.04288483e+02,  1.75345154e+01, -4.03978271e+01,
        1.17001984e+02, -2.23410034e+01,  7.62485352e+01,  9.95090027e+01,
       -6.25500793e+01, -6.23908691e+01,  3.89622498e+01,  8.02760315e+01,
       -8.80406799e+01, -3.50216858e+02, -4.16336670e+01,  3.23754883e+01,
        1.47501221e+01,  1.26898743e+02,  4.58666992e+00, -2.09291382e+01,
       -4.63806152e-01, -7.16840820e+01,  6.68127441e+01, -4.77747803e+01,
       -2.39722107e+02, -1.82174377e+02, -1.14288452e+02,  8.92976074e+01,
       -1.76489258e+00,  7.19572144e+01,  4.34899902e+00, -6.35200806e+01,
        3.36867676e+01, -1.09863281e-02, -3.13136353e+02, -3.04594727e+01,
       -2.03292603e+02, -6.26403809e+00,  2.49539795e+01,  8.23956299e+01,
       -2.02749023e+01,  6.30859375e-01,  9.74741211e+01, -3.07237549e+01,
        2.30983887e+01, -1.05333008e+02, -4.74336426e+02, -1.04421387e+02,
        1.18629150e+02,  4.91618652e+01,  1.77366943e+01, -5.09847412e+01,
        6.53220215e+01,  5.60297852e+01,  2.20312500e+01, -2.14761841e+02,
       -7.57222900e+01,  5.24270020e+01,  2.35379639e+01, -5.40844727e+00,
        9.83039551e+01, -1.90515137e+01,  1.13490601e+02, -1.22815186e+02,
       -3.75110596e+02,  2.94479980e+01, -8.11767578e+00,  2.30630371e+02,
       -2.21856934e+02,  1.98024902e+02, -7.98215332e+01,  4.95251465e+01,
       -2.07468262e+01, -2.14999023e+02, -3.74052734e+01, -8.99821777e+01,
        4.27360840e+01,  4.12053223e+01,  3.22509766e+01,  1.75200195e+01,
        6.32805176e+01, -5.16687012e+01, -2.10808594e+02, -1.83963867e+02,
        1.11079346e+02, -1.16482422e+02,  2.03957275e+02, -7.33830566e+01,
        1.47921143e+02, -9.34592285e+01, -3.33400879e+01, -4.14875488e+01,
       -6.58161621e+01, -6.06201172e+00,  1.03962646e+02, -9.55339355e+01,
       -4.34025879e+01, -7.14077148e+01, -1.37099609e+01,  6.51933594e+01,
       -2.58312988e+01,  2.11472168e+01, -2.47263672e+02, -1.89649170e+02,
       -7.08708496e+01, -5.19438477e+01,  6.52338867e+01,  1.65212402e+01,
       -7.45874023e+01,  4.24604492e+01, -8.08146973e+01, -8.29882812e+00,
        6.45378418e+01, -3.11279297e+00, -1.39932861e+02, -1.78218018e+02,
        5.48134766e+01,  8.66081543e+01,  6.78093262e+01, -9.07756348e+01,
        9.45754395e+01,  1.15795898e+00])

I want to make a boxplot of the data with the month on the x-axis and the data on the y-axis. This is fairly simple in Matlab, where you can just use one line to plot it:

boxchart(date_month,delta_gmb_gris)

which produces exactly what I want:

如何制作一个箱线图,以月份为X轴,数据为Y轴。

In Python, however, this is not the case:

data = [date_month,delta_gmb_gris]
fig = plt.figure(figsize =(10, 7))
# Creating axes instance
ax = fig.add_axes([0, 0, 1, 1])
# Creating plot
bp = ax.boxplot(data)
# show plot
plt.show()

如何制作一个箱线图,以月份为X轴,数据为Y轴。

How can I solve it?

答案1

得分: 1

You can use either seaborn or just matplotlib.

First, convert data to a pandas dataframe...

import pandas as pd
df=pd.DataFrame({'date_month': date_month, 'delta_gmb_gris':delta_gmb_gris})

then plot using seaborn...

import seaborn as sns
sns.boxplot(data=df, x='date_month', y='delta_gmb_gris')

如何制作一个箱线图,以月份为X轴,数据为Y轴。

If you want to use matplotlib only...

df.boxplot(column='delta_gmb_gris', by='date_month')

如何制作一个箱线图,以月份为X轴,数据为Y轴。

英文:

You can use either seaborn or just matplotlib.

First, convert data to a pandas dataframe...

import pandas as pd
df=pd.DataFrame({'date_month': date_month, 'delta_gmb_gris':delta_gmb_gris})

then plot using seaborn...

import seaborn as sns
sns.boxplot(data=df, x='date_month', y='delta_gmb_gris')

如何制作一个箱线图,以月份为X轴,数据为Y轴。

If you want to use matplotlib only...

df.boxplot(column='delta_gmb_gris', by='date_month')

如何制作一个箱线图,以月份为X轴,数据为Y轴。

huangapple
  • 本文由 发表于 2023年5月25日 18:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定