Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有相同数据的Matplotlib图形不';t重叠_Python_Matplotlib - Fatal编程技术网

Python 具有相同数据的Matplotlib图形不';t重叠

Python 具有相同数据的Matplotlib图形不';t重叠,python,matplotlib,Python,Matplotlib,我已经生成了一些数据,并尝试将它们可视化为同一绘图中的两个图形。一个是酒吧,另一个是线 然而,由于某些原因,这些图表似乎没有重叠 这是我的密码: 这是图表 数据集是相同的,但是折线图向右移动了两个数据点(从4开始,而不是从2开始)。如果我分别绘制它们,它们将正确显示(都从2开始)。那么如果我把它们画在同一张图上会有什么不同呢?如何解决这个问题 我找不到比重新提供x轴数据更简单的方法。如果这是您正在使用的更大方法的代表,那么您可能需要从pd.Series()绘制此数据,而不是使用列表,但此代码

我已经生成了一些数据,并尝试将它们可视化为同一绘图中的两个图形。一个是酒吧,另一个是线

然而,由于某些原因,这些图表似乎没有重叠

这是我的密码:

这是图表


数据集是相同的,但是折线图向右移动了两个数据点(从4开始,而不是从2开始)。如果我分别绘制它们,它们将正确显示(都从2开始)。那么如果我把它们画在同一张图上会有什么不同呢?如何解决这个问题

我找不到比重新提供x轴数据更简单的方法。如果这是您正在使用的更大方法的代表,那么您可能需要从
pd.Series()
绘制此数据,而不是使用列表,但此代码至少会为您提供所需的绘图。如果您使用的是Python 3,请将
iterms()
更改为
items()

似乎x轴的某些自动缩放发生在线形图之后,这将使两个图失去同步两个点(可能的最小值)。在绘制两个图之前,可能会禁用x轴上的自动缩放,但这似乎更困难

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

# roll two 6-sided dices 500 times
dice_1 = pd.Series(np.random.randint(1, 7, 500))
dice_2 = pd.Series(np.random.randint(1, 7, 500))

dices = dice_1 + dice_2

# plotting the requency of a 2 times 6 sided dice role
fc = collections.Counter(dices)

x_axis = [key for key, value in fc.iteritems()]
y_axis = [value for key, value in fc.iteritems()]

plt.plot(x_axis, y_axis, alpha=0.6, linestyle='-', marker='o')
plt.bar(x_axis, y_axis, color='k', alpha=0.6, align='center')
plt.show()

这是因为序列图使用索引,将
use\u index设置为
False
将解决问题,我还建议使用
groupby
len
来计算每个组合的频率

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

# roll two 6-sided dices 500 times
dice_1 = pd.Series(np.random.randint(1, 7, 500))
dice_2 = pd.Series(np.random.randint(1, 7, 500))
dices = dice_1 + dice_2

# returns the corresponding value of each index from dices
func = lambda x: dices.loc[x]

fc = dices.groupby(func).agg({'count': len})

ax = fc.plot(kind='line', alpha=0.6, linestyle='-',
             marker='o', use_index=False)
fc.plot(ax=ax, kind='bar', alpha=0.6, color='k')

plt.show()
结果如下所示

我认为,乔·金顿的答案中的编辑描述了这个问题。然而,现在已经5岁了,因为我怀疑这是一种可取的行为,我想知道是否有一个很好的解决办法。还在看。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# roll two 6-sided dices 500 times
dice_1 = pd.Series(np.random.randint(1, 7, 500))
dice_2 = pd.Series(np.random.randint(1, 7, 500))
dices = dice_1 + dice_2

# returns the corresponding value of each index from dices
func = lambda x: dices.loc[x]

fc = dices.groupby(func).agg({'count': len})

ax = fc.plot(kind='line', alpha=0.6, linestyle='-',
             marker='o', use_index=False)
fc.plot(ax=ax, kind='bar', alpha=0.6, color='k')

plt.show()