저는 plotly express와 gapminder 데이터 셋을 사용하여 비슷한 데이터 셋이있는 막대 차트를 플로팅하고 있습니다. 공식 웹 사이트에서 지침을 찾았으며 여기에 코드와 그림이 있습니다.
import plotly.express as px
gapminder = px.data.gapminder()
fig = px.bar(gapminder, x="continent", y="pop", color="continent",
animation_frame="year", animation_group="country", range_y=[0,4000000000])
fig.show()
그러나 비교를 위해 각 막대에 대해 다른 데이터 세트를 추가하고 싶습니다. 이상적으로는 다음과 같은 결과를 얻고 싶습니다.
For the result in each bar, I have a dataset as the "total number". For example, for the first bar, the "yellow one" is the total number and "blue one" is the targeted number, just I wish to combine them in the same figure. But the example only shows a single dataset. I wonder how I can change the parameter of "y" to include the two datasets?
If, as you say, you've got a similar dataset, then you don't need to change the parameter of y
to include two datasets. You only have to add another dimension to your existing dataset and adjust how you're using the x
and color
arguments. In your example you're assigning continent
to both x
and color
. So you're indicating the different continents in two ways. And you don't have to do that. You can use color
to split two datasets and still keep the population data illustrated on different contintents. I'll show you how to do that.
Gapminder
5 개 대륙 모두에 대한 데이터가 있습니다. 다음 스 니펫은 gapminder 데이터를 자체에 추가하고 새 열 planet
을 사용 하여 결합 된 데이터 세트를 둘로 분할합니다. 이렇게하면 같은 대륙을 가진 두 개의 행성을 갖게됩니다. 두 행성에 대해 동일한 대륙이있는 한 완벽하게 작동합니다.
음모:
암호:
import plotly.express as px
import pandas as pd
gapminder = px.data.gapminder()
gapminder2=gapminder.copy(deep=True)
gapminder['planet']='earth'
gapminder2['planet']='mars'
gapminder3=pd.concat([gapminder, gapminder2])
fig = px.bar(gapminder3, x="continent", y="pop", color="planet",
animation_frame="year", animation_group="country", range_y=[0,4000000000*2])
fig.show()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다