Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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 同一可视化中具有不同y轴的2个线图_Python_Pandas_Plot_Spyder - Fatal编程技术网

Python 同一可视化中具有不同y轴的2个线图

Python 同一可视化中具有不同y轴的2个线图,python,pandas,plot,spyder,Python,Pandas,Plot,Spyder,我对这件事很感兴趣。 我试图创建一个具有相同x轴比例(年)但不同y轴比例的折线图 我该怎么做 我有两个包含此信息的数据集: test_data1.head() latitude longitude place year 0 36.087 -106.168 New Mexico 1973 1 33.917 -90.775 Mississippi 1973 2 37.160 -104.594 Colorado 1973 3 37

我对这件事很感兴趣。 我试图创建一个具有相同x轴比例(年)但不同y轴比例的折线图 我该怎么做

我有两个包含此信息的数据集:

test_data1.head()
   latitude longitude   place       year
0   36.087  -106.168    New Mexico  1973
1   33.917  -90.775     Mississippi 1973
2   37.160  -104.594    Colorado    1973
3   37.148  -104.571    Colorado    1973
4   36.500  -100.693    Oklahoma    1974

test_data2.head()
    LAT          LONG       BBLS    Year
0   36.900324   -98.21826   300     1977.0
1   36.896636   -98.17772   1,000   2002.0
2   36.806113   -98.32584   1,000   1988.0
3   36.888589   -98.31853   1,000   1985.0
4   36.892128   -98.19462   2,400   2002.0
上下文中,测试数据1指地震,测试数据2指注入井,BBL指生产的桶数

我想做的是创建一个折线图,显示每年地震的次数,以及1980年以来每年生产的桶数。我想在左边(y1)显示每年地震的次数,在右边(y2)显示每年生产的桶数

这是我做过的但没用的,有人知道为什么或者告诉我怎么做吗

import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
%matplotlib  inline 

x=test_data1['year']
y1 = test_data1['place']
y2 = test_data2['BBLS']

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
curve1 = ax1.plot (x, y1, label='Number of Earthquakes', color='r')
curve2= ax2.plot (x, y2, label='Number of Barrels Produced', color='b')
plt.plot()
plt.show()
我遇到了这个巨大的错误,但我无法理解,因为我是Python新手:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-109-9e26d5f7e5e2> in <module>
     11 ax2 = ax1.twinx()
     12 curve1 = ax1.plot (x, y1, label='Number of Earthquakes', color='r')
---> 13 curve2= ax2.plot (x, y2, label='Number of Barrels Produced', color='b')
     14 plt.plot()
     15 plt.show()

~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1741         """
   1742         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1744         for line in lines:
   1745             self.add_line(line)

~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
    271                 this += args[0],
    272                 args = args[1:]
--> 273             yield from self._plot_args(this, kwargs)
    274 
    275     def get_next_color(self):

~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    394             self.axes.xaxis.update_units(x)
    395         if self.axes.yaxis is not None:
--> 396             self.axes.yaxis.update_units(y)
    397 
    398         if x.shape[0] != y.shape[0]:

~\anaconda3\lib\site-packages\matplotlib\axis.py in update_units(self, data)
   1461         neednew = self.converter != converter
   1462         self.converter = converter
-> 1463         default = self.converter.default_units(data, self)
   1464         if default is not None and self.units is None:
   1465             self.set_units(default)

~\anaconda3\lib\site-packages\matplotlib\category.py in default_units(data, axis)
    105         # the conversion call stack is default_units -> axis_info -> convert
    106         if axis.units is None:
--> 107             axis.set_units(UnitData(data))
    108         else:
    109             axis.units.update(data)

~\anaconda3\lib\site-packages\matplotlib\category.py in __init__(self, data)
    174         self._counter = itertools.count()
    175         if data is not None:
--> 176             self.update(data)
    177 
    178     @staticmethod

~\anaconda3\lib\site-packages\matplotlib\category.py in update(self, data)
    209         for val in OrderedDict.fromkeys(data):
    210             # OrderedDict just iterates over unique values in data.
--> 211             cbook._check_isinstance((str, bytes), value=val)
    212             if convertible:
    213                 # this will only be called so long as convertible is True.

~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in _check_isinstance(_types, **kwargs)
   2244     for k, v in kwargs.items():
   2245         if not isinstance(v, types):
-> 2246             raise TypeError(
   2247                 "{!r} must be an instance of {}, not a {}".format(
   2248                     k,

TypeError: 'value' must be an instance of str or bytes, not a float
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
11 ax2=ax1.twinx()
12曲线1=ax1.plot(x,y1,标签为地震次数,颜色为r)
--->13曲线2=ax2.plot(x,y2,标签='生产的桶数',颜色='b')
14 plt.绘图()
15 plt.show()
绘图中的~\anaconda3\lib\site packages\matplotlib\axes\\u axes.py(self、scalex、scaley、data、*args、**kwargs)
1741         """
1742 kwargs=cbook.normalize_kwargs(kwargs,mlines.Line2D)
->1743行=[*self.\u get\u行(*args,data=data,**kwargs)]
1744对于行中的行:
1745自我添加_行(行)
调用中的~\anaconda3\lib\site packages\matplotlib\axes\\u base.py(self、data、*args、**kwargs)
271 this+=args[0],
272 args=args[1:]
-->273自绘制的收益率(本,kwargs)
274
275 def get_next_颜色(自身):
参数中的~\anaconda3\lib\site packages\matplotlib\axes\\u base.py(self、tup、kwargs)
394自轴x轴更新单位(x)
395如果self.axes.yaxis不是无:
-->396自轴y轴更新单位(y)
397
398如果x.shape[0]!=y.shape[0]:
~\anaconda3\lib\site packages\matplotlib\axis.py,单位为更新单位(自身,数据)
1461 neednew=自转换器!=转换器
1462自转换器=转换器
->1463 default=self.converter.default\u单位(数据,self)
1464如果默认值不是None且self.units为None:
1465自我设置单位(默认)
~\anaconda3\lib\site packages\matplotlib\category.py默认单位(数据,轴)
105#转换调用堆栈为默认的_单位->轴_信息->转换
106如果axis.units为无:
-->107轴。设置单位(单位数据(数据))
108.其他:
109轴单位更新(数据)
~\anaconda3\lib\site packages\matplotlib\category.py in\uuuuuu init\uuuuuuu(self,data)
174 self.\u counter=itertools.count()
175如果数据不是无:
-->176.自我更新(数据)
177
178@staticmethod
更新中的~\anaconda3\lib\site packages\matplotlib\category.py(self,data)
209对于订单中的val信息,从键(数据):
210#OrderedDict只对数据中的唯一值进行迭代。
-->211 cbook.\u check\u isinstance((str,字节),value=val)
212如果可转换:
213#只有在convertible为真的情况下,才会称之为convertible。
~\anaconda3\lib\site packages\matplotlib\cbook\\uuuu init\uuuu.py in\u check\u isinstance(\u类型,**kwargs)
2244表示k,v,单位为kwargs.items():
2245如果不存在(v,类型):
->2246上升类型错误(
2247“{!r}必须是{}的实例,而不是{}”。格式(
2248K,
TypeError:“value”必须是str或bytes的实例,而不是float
尽管如此,Jupiter还是生成了一些ofc不是我想要的图,更不用说我获得了y1的位置,但我确实想计算一年中的地震数量,我只是觉得使用“位置”会让代码只是计算。 请帮忙

以下是我正在处理的数据集,以防有用。请注意,它们有更多的列,但我不使用它们,因为它们与此图形无关

(测试数据1)
(test_data2)

在创建之前用
sharex
像这样共享axis

y2 = test_data2['BBLS'].astype(str)

fig=plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)

curve1 = ax1.plot (x, y1, label='Number of Earthquakes', color='r')
curve2= ax2.plot (x, y2, label='Number of Barrels Produced', color='b')
plt.plot()
plt.show()

我仍然得到相同的错误类型错误:“value”必须是str或bytes的实例,而不是更新的floatAnswer,你应该先转换你的y2。我认为现在有其他错误。你知道吗?我现在得到value错误:x和y必须具有相同的第一维度,但具有形状(13946)和(11065)