pandas 在一个轴上挤压图 [英] Pandas squished plot on one axis

查看:27
本文介绍了 pandas 在一个轴上挤压图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框,需要在一个轴上绘制.不幸的是,显示的数据和 xticks 之一向左移动.

I have two data frames with i need to plot in one axis. Unfortunately one of displayed data and xticks are shifted to the left.

测试示例:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame()
df['x'] = np.linspace(20, 30, 10)
df['y'] = np.random.randint(1, 10, 10)

df2 = pd.DataFrame()
df2['x'] = np.linspace(1, 40, 50)
df2['y'] = np.random.randint(1, 10, 50)

fig, ax = plt.subplots()
df.plot(x='x', y='y', kind='bar', ax=ax, width=1., figsize=(3, 2.5), legend=None)
df2.plot(x='x', y='y', kind='line', ax=ax, legend=None, color='red')

plt.show()

结果:

问题:如何在一个轴上正确显示这些数据?

Question: How to proper display this data on one axis?

推荐答案

我只能通过在 matplotlib 中直接做绘图来解决这个问题:

I was only able to solve this by doing the plot in matplotlib directly:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame()
df['x'] = np.linspace(20, 30, 10)
df['y'] = np.random.randint(1, 10, 10)

df2 = pd.DataFrame()
df2['x'] = np.linspace(1, 40, 50)
df2['y'] = np.random.randint(1, 10, 50)

fig, ax = plt.subplots()

ax.bar(x=df.x, height=df.y)
ax.plot(df2.set_index('x'), c='red')

这篇关于 pandas 在一个轴上挤压图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆