Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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 3.x 使用pandas和matplotlib创建具有不同颜色的单个条形图_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

Python 3.x 使用pandas和matplotlib创建具有不同颜色的单个条形图

Python 3.x 使用pandas和matplotlib创建具有不同颜色的单个条形图,python-3.x,pandas,matplotlib,Python 3.x,Pandas,Matplotlib,我有两个dfs,我想为其创建一个条形图, 每个条都需要它自己的颜色,这取决于它来自哪个df # Ages < 20 df1.tags = ['locari', 'ママコーデ', 'ponte_fashion', 'kurashiru', 'fashion'] df1.tag_count = [2162, 1647, 1443, 1173, 1032] # Ages 20 - 24 df2.tags= ['instagood', 'ootd', 'fashion', 'followme',

我有两个dfs,我想为其创建一个条形图, 每个条都需要它自己的颜色,这取决于它来自哪个df

# Ages < 20
df1.tags = ['locari', 'ママコーデ', 'ponte_fashion', 'kurashiru', 'fashion']
df1.tag_count = [2162, 1647, 1443, 1173, 1032]

# Ages 20 - 24
df2.tags= ['instagood', 'ootd', 'fashion', 'followme', 'love']
df2.tag_count = [6523, 4576, 3986, 3847, 3599]
#年龄<20岁
df1.tags=['locari','ママコーデ', 'ponte_fashion“、”kurashiru“、”fashion“]
df1.tag_count=[2162164714431731032]
#20-24岁
标签=['instagood','ootd','fashion','followme','love']
df2.tag_count=[65234576398638473599]
我如何创建这样一个情节


另外,原来的df要大得多。有些单词可能会重叠,但我希望它们也有不同的颜色

您的数据帧标记计数只是简单的列表,因此您可以使用标准mpl条形图在同一轴上绘制它们。这个答案假设两个数据帧具有相同的长度

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

# Create dataframes
df1=pd.DataFrame()
df2=pd.DataFrame()

# Ages < 20
df1.tags = ['locari', 'blub', 'ponte_fashion', 'kurashiru', 'fashion']
df1.tag_count = [2162, 1647, 1443, 1173, 1032]

# Ages 20 - 24
df2.tags= ['instagood', 'ootd', 'fashion', 'followme', 'love']
df2.tag_count = [6523, 4576, 3986, 3847, 3599]

# Create figure
fig=plt.figure()
ax=fig.add_subplot(111)

# x-coordinates
ind1 = np.arange(len(df1.tag_count))
ind2 = np.arange(len(df2.tag_count))
width = 0.35

# Bar plot for df1
ax.bar(ind1,df1.tag_count,width,color='r')

# Bar plot for df1
ax.bar(ind2+width,df2.tag_count,width,color='b')

# Create new xticks
ticks=list(ind1+0.5*width)+list(ind2+1.5*width)
ticks.sort()
ax.set_xticks(ticks)

# Sort labels in an alternating way
labels = [None]*(len(df1.tags)+len(df2.tags))
labels[::2] = df1.tags
labels[1::2] = df2.tags
ax.set_xticklabels(labels)

plt.show()
导入matplotlib.pyplot作为plt
作为pd进口熊猫
将numpy作为np导入
#创建数据帧
df1=pd.DataFrame()
df2=pd.DataFrame()
#年龄<20岁
df1.tags=['locari'、'blub'、'ponte_fashion'、'kurashiru'、'fashion']
df1.tag_count=[2162164714431731032]
#20-24岁
标签=['instagood','ootd','fashion','followme','love']
df2.tag_count=[65234576398638473599]
#塑造形象
图=plt.图()
ax=图添加_子批次(111)
#x坐标
ind1=np.arange(len(df1.tag_计数))
ind2=np.arange(len(df2.tag\u计数))
宽度=0.35
#df1的条形图
ax.bar(ind1,df1.tag_count,width,color='r')
#df1的条形图
最大条(ind2+宽度,df2.标记计数,宽度,颜色='b')
#创建新的xtick
刻度=列表(ind1+0.5*宽度)+列表(ind2+1.5*宽度)
ticks.sort()
ax.set_xticks(刻度)
#以交替方式对标签进行排序
标签=[None]*(len(df1.tags)+len(df2.tags))
标签[::2]=df1.tags
标签[1::2]=df2.tags
ax.set_xticklabel(标签)
plt.show()
这将返回一个这样的绘图


请注意,要将两个
标记
合并到一个列表中,我假设两个列表的长度相同

您的数据帧标记计数只是简单的列表,因此您可以使用标准mpl条形图在同一轴上绘制它们。这个答案假设两个数据帧具有相同的长度

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

# Create dataframes
df1=pd.DataFrame()
df2=pd.DataFrame()

# Ages < 20
df1.tags = ['locari', 'blub', 'ponte_fashion', 'kurashiru', 'fashion']
df1.tag_count = [2162, 1647, 1443, 1173, 1032]

# Ages 20 - 24
df2.tags= ['instagood', 'ootd', 'fashion', 'followme', 'love']
df2.tag_count = [6523, 4576, 3986, 3847, 3599]

# Create figure
fig=plt.figure()
ax=fig.add_subplot(111)

# x-coordinates
ind1 = np.arange(len(df1.tag_count))
ind2 = np.arange(len(df2.tag_count))
width = 0.35

# Bar plot for df1
ax.bar(ind1,df1.tag_count,width,color='r')

# Bar plot for df1
ax.bar(ind2+width,df2.tag_count,width,color='b')

# Create new xticks
ticks=list(ind1+0.5*width)+list(ind2+1.5*width)
ticks.sort()
ax.set_xticks(ticks)

# Sort labels in an alternating way
labels = [None]*(len(df1.tags)+len(df2.tags))
labels[::2] = df1.tags
labels[1::2] = df2.tags
ax.set_xticklabels(labels)

plt.show()
导入matplotlib.pyplot作为plt
作为pd进口熊猫
将numpy作为np导入
#创建数据帧
df1=pd.DataFrame()
df2=pd.DataFrame()
#年龄<20岁
df1.tags=['locari'、'blub'、'ponte_fashion'、'kurashiru'、'fashion']
df1.tag_count=[2162164714431731032]
#20-24岁
标签=['instagood','ootd','fashion','followme','love']
df2.tag_count=[65234576398638473599]
#塑造形象
图=plt.图()
ax=图添加_子批次(111)
#x坐标
ind1=np.arange(len(df1.tag_计数))
ind2=np.arange(len(df2.tag\u计数))
宽度=0.35
#df1的条形图
ax.bar(ind1,df1.tag_count,width,color='r')
#df1的条形图
最大条(ind2+宽度,df2.标记计数,宽度,颜色='b')
#创建新的xtick
刻度=列表(ind1+0.5*宽度)+列表(ind2+1.5*宽度)
ticks.sort()
ax.set_xticks(刻度)
#以交替方式对标签进行排序
标签=[None]*(len(df1.tags)+len(df2.tags))
标签[::2]=df1.tags
标签[1::2]=df2.tags
ax.set_xticklabel(标签)
plt.show()
这将返回一个这样的绘图

请注意,要将两个
标记
合并到一个列表中,我假设两个列表的长度相同

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

# Create dataframes
df1=pd.DataFrame()
df2=pd.DataFrame()

# Ages < 20
df1.tags = ['locari', 'blub', 'ponte_fashion', 'kurashiru', 'fashion']
df1.tag_count = [2162, 1647, 1443, 1173, 1032]

# Ages 20 - 24
df2.tags= ['instagood', 'ootd', 'fashion', 'followme', 'love']
df2.tag_count = [6523, 4576, 3986, 3847, 3599]

# Create figure
fig=plt.figure()
ax=fig.add_subplot(111)

# x-coordinates
ind1 = np.arange(len(df1.tag_count))
ind2 = np.arange(len(df2.tag_count))
width = 0.35

# Bar plot for df1
ax.bar(ind1,df1.tag_count,width,color='r')

# Bar plot for df1
ax.bar(ind2+width,df2.tag_count,width,color='b')

# Create new xticks
ticks=list(ind1+0.5*width)+list(ind2+1.5*width)
ticks.sort()
ax.set_xticks(ticks)

# Sort labels in an alternating way
labels = [None]*(len(df1.tags)+len(df2.tags))
labels[::2] = df1.tags
labels[1::2] = df2.tags
ax.set_xticklabels(labels)

plt.show()