matplotlib 如何在twinx / secondary_y上使用nan绘制多列线图

2lpgd968  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(117)

该图是固定的,但我现在有麻烦绘制的图例。它只显示图例为1的情节。如下图所示
我试图绘制一个双轴图与twinx,但我面临着一些困难,如下图所示。
任何输入是受欢迎的!如果您需要任何额外的信息,我很乐意提供给你。
x1c 0d1x的数据
与绘制z轴之前的原始图像相比。



我不确定为什么我的图是这样的,因为在绘制第二个y轴(粉红色的线)之前,可以完美地看到关闭值图,但现在它似乎被切断了。
这可能是由于我下面提供的数据。
我目前拥有的代码:

# read csv into variable
sg_df_merged = pd.read_csv("testing1.csv", parse_dates=[0], index_col=0)

# define figure
fig = plt.figure()

fig, ax5 = plt.subplots()
ax6 = ax5.twinx()

x = sg_df_merged.index
y = sg_df_merged["Adj Close"]
z = sg_df_merged["Singapore"]

curve1 = ax5.plot(x, y, label="Singapore", color = "c")
curve2 = ax6.plot(x, z, label = "Face Mask Compliance", color = "m")
curves = [curve1, curve2]

# labels for my axis
ax5.set_xlabel("Year")
ax5.set_ylabel("Adjusted Closing Value ($)")
ax6.set_ylabel("% compliance to wearing face mask")
ax5.grid #not sure what this line does actually

# set x-axis values to 45 degree angle
for label in ax5.xaxis.get_ticklabels():
    label.set_rotation(45)
ax5.grid(True, color = "k", linestyle = "-", linewidth = 0.3)

plt.gca().legend(loc='center left', bbox_to_anchor=(1.1, 0.5), title = "Country Index")
plt.show();

字符串
最初,我以为这是由于我的excel有整个空白行,但我已经删除了这些行。示例数据是在this question中。
此外,我试图插值,但不知何故,它不工作。

eoigrqb6

eoigrqb61#

  • 只删除了所有NaN的行。仍然有很多NaN的行。
  • 为了让matplotlib在两个数据点之间绘制连接线,这些点必须是连续的。
  • 绘图API未连接NaN值之间的数据
  • 这可以通过将pandas.Series转换为DataFrame并使用.dropna来处理。
  • 看到x已经被删除了,因为它将不匹配yz的索引长度。它们在.dropna之后更短。
  • y现在是一个单独的嵌入式框架,其中使用.dropna
  • z也是一个单独的嵌入式框架,其中使用.dropna
  • 图中的x-axis是各自的索引。
    *python v3.12.0pandas v2.1.2matplotlib v3.8.1中测试。
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# read csv into variable
sg_df_merged = pd.read_csv("test.csv", parse_dates=[0], index_col=0)

# define figure
fig, ax5 = plt.subplots(figsize=(8, 6))
ax6 = ax5.twinx()

# select specific columns to plot and drop additional NaN
y = pd.DataFrame(sg_df_merged["Adj Close"]).dropna()
z = pd.DataFrame(sg_df_merged["Singapore"]).dropna()

# add plots with markers
curve1 = ax5.plot(y.index, 'Adj Close', data=y, label="Singapore", color = "c", marker='o')
curve2 = ax6.plot(z.index, 'Singapore', data=z, label = "Face Mask Compliance", color = "m", marker='o')

# labels for my axis
ax5.set_xlabel("Year")
ax5.set_ylabel("Adjusted Closing Value ($)")
ax6.set_ylabel("% compliance to wearing face mask")
    
# rotate xticks
ax5.xaxis.set_tick_params(rotation=45)

# add a grid to ax5
ax5.grid(True, color = "k", linestyle = "-", linewidth = 0.3)

# create a legend for both axes
curves = curve1 + curve2
labels = [l.get_label() for l in curves]
ax5.legend(curves, labels, loc='center left', bbox_to_anchor=(1.1, 0.5), title = "Country Index")

plt.show()

字符串


的数据

# given a datetime[ns] dtype index, if the time components are all 0, extracting only the date will cause the xticklabels to be centered under the tick
df.index = df.index.date

ax = df['Adj Close'].dropna().plot(marker='.', color='c', grid=True, figsize=(12, 6),
                                   title='My Plot', ylabel='Adj Close', xlabel='Date', legend='Adj Close')
ax_right = df['Singapore'].dropna().plot(marker='.', color='m', secondary_y=True, legend='Singapore', rot=0, ax=ax)

ax_right.set_ylabel('Singapore')

ax.legend(title='Country Index', bbox_to_anchor=(1.06, 0.5), loc='center left', frameon=False)
ax_right.legend(bbox_to_anchor=(1.06, 0.43), loc='center left', frameon=False)

ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
ax.xaxis.set_minor_locator(mdates.MonthLocator())


数据

  • 将数据复制到剪贴板,并使用以下行读取。
df = pd.read_clipboard(sep=',', index_col=[0], parse_dates=[0])
,Adj Close,Singapore
2015-10-01,2998.350098,
2015-11-01,2855.939941,
2015-12-01,2882.72998,
2016-01-01,2629.110107,
2016-02-01,2666.51001,
2016-03-01,2840.899902,
2016-04-01,2838.52002,
2016-05-01,2791.060059,
2016-06-01,2840.929932,
2016-07-01,2868.689941,
2016-08-01,2820.590088,
2016-09-01,2869.469971,
2016-10-01,2813.8701170000004,
2016-11-01,2905.169922,
2016-12-01,2880.76001,
2017-01-01,3046.800049,
2017-02-01,3096.610107,
2017-03-01,3175.110107,
2017-04-01,3175.439941,
2017-05-01,3210.820068,
2017-06-01,3226.47998,
2017-07-01,3329.52002,
2017-08-01,3277.26001,
2017-09-01,3219.909912,
2017-10-01,3374.080078,
2017-11-01,3433.540039,
2017-12-01,3402.919922,
2018-01-01,3533.98999,
2018-02-01,3517.939941,
2018-03-01,3427.969971,
2018-04-01,3613.929932,
2018-05-01,3428.179932,
2018-06-01,3268.699951,
2018-07-01,3319.850098,
2018-08-01,3213.47998,
2018-09-01,3257.050049,
2018-10-01,3018.800049,
2018-11-01,3117.610107,
2018-12-01,3068.76001,
2019-01-01,3190.169922,
2019-02-01,3212.689941,
2019-03-01,3212.879883,
2019-04-01,3400.199951,
2019-05-01,3117.76001,
2019-06-01,3321.610107,
2019-07-01,3300.75,
2019-08-01,3106.52002,
2019-09-01,3119.98999,
2019-10-01,3229.879883,
2019-11-01,3193.919922,
2019-12-01,3222.830078,
2020-01-01,3153.72998,
2020-02-01,3011.080078,
2020-02-21,,24.0
2020-02-25,,
2020-02-28,,22.0
2020-03-01,2481.22998,
2020-03-02,,
2020-03-03,,
2020-03-06,,23.0
2020-03-10,,
2020-03-13,,21.0
2020-03-17,,
2020-03-20,,24.0
2020-03-23,,
2020-03-24,,
2020-03-27,,27.0
2020-03-30,,
2020-03-31,,
2020-04-01,2624.22998,
2020-04-03,,37.0
2020-04-06,,
2020-04-07,,
2020-04-10,,73.0
2020-04-13,,
2020-04-14,,
2020-04-17,,85.0
2020-04-20,,
2020-04-21,,
2020-04-24,,90.0
2020-04-27,,
2020-04-28,,
2020-05-01,2510.75,90.0
2020-05-05,,
2020-05-15,,
2020-05-21,,
2020-05-22,,92.0
2020-05-25,,
2020-05-26,,
2020-05-30,,
2020-06-01,2589.909912,
2020-06-05,,89.0
2020-06-08,,
2020-06-15,,
2020-06-16,,
2020-06-19,,92.0
2020-06-22,,
2020-06-25,,
2020-07-01,2529.820068,
2020-07-03,,
2020-07-06,,
2020-07-07,,90.0
2020-07-12,,
2020-07-14,,
2020-07-20,,92.0
2020-07-26,,
2020-07-27,,
2020-07-31,,
2020-08-01,2532.51001,
2020-08-03,,88.0
2020-08-07,,
2020-08-10,,
2020-08-12,,
2020-08-14,,90.0
2020-08-17,,
2020-08-25,,
2020-08-28,,90.0
2020-08-31,,
2020-09-01,2490.090088,
2020-09-11,2490.090088,

的字符串

相关问题