简体   繁体   English

Matplotlib开关X和Y轴

[英]Matplotlib Switch X and Y Axis

I have a seemingly simple question that I can't figure out. 我有一个看似简单的问题,我不知道。 The code below products the chart below. 下面的代码生成下面的图表。

df = pd.DataFrame({.5:[0,0,0], .6:[1,2,1], .7:[7,8,6], .8:[23,33,21], .9: 
[84,126,76] }, index=['Desktop', 'Mobile', 'Table'])
df.T.plot(figsize=(10,10))

在此处输入图片说明

This is almost what I want, but I need percentile on the Y-Axis and Days on the X-Axis. 这几乎是我想要的,但是我需要在Y轴上显示百分位数,在X轴上显示天数。 Thanks for the help. 谢谢您的帮助。

You need to clean up your data into a tidy format, then specify your matplotlib axes, and the x- & y- values explicitly: 您需要将数据整理整齐的格式,然后指定matplotlib轴,并明确指定x-和y-值:

from matplotlib import pyplot
import pandas

df = pandas.DataFrame({
    0.5: [0, 0, 0],
    0.6: [1, 2, 1],
    0.7: [7, 8, 6],
    0.8: [23, 33, 21],
    0.9: [84, 126, 76]
}, index=['Desktop', 'Mobile', 'Tablet'])

fig, ax = pyplot.subplots()
groups = (
    df.T
      .stack()
      .rename_axis(['pctile', 'device'])
      .to_frame('days')
      .reset_index()
      .groupby(by=['device'])
)
for device, g in groups:
    g.plot.line(ax=ax, x='days', y='pctile', label=device)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM