Python 2.7 如何在Python(Pandas或matplotlib)中的条形图中移动对数轴刻度位置(10)
在Y轴(对数刻度)中,为什么0-10范围比其他范围(10-100、100-1000等)小。是否有方法调整x记号标记的位置和值?我想清楚地显示较小的值 我的脚本是:Python 2.7 如何在Python(Pandas或matplotlib)中的条形图中移动对数轴刻度位置(10),python-2.7,pandas,matplotlib,bar-chart,yaxis,Python 2.7,Pandas,Matplotlib,Bar Chart,Yaxis,在Y轴(对数刻度)中,为什么0-10范围比其他范围(10-100、100-1000等)小。是否有方法调整x记号标记的位置和值?我想清楚地显示较小的值 我的脚本是: 可以使用ax.set_ylim()或plt.ylim()设置y轴的限制。显然0不能是对数刻度上的限制,因此需要使用一些正数,例如ax.set_ylim((1e-1,无)) word_freqs, words ([[7637.78430956, 1938.76578683, 208.902929772, 40.3146004823,
可以使用
ax.set_ylim()
或plt.ylim()
设置y轴的限制。显然0
不能是对数刻度上的限制,因此需要使用一些正数,例如ax.set_ylim((1e-1,无))
word_freqs, words
([[7637.78430956, 1938.76578683, 208.902929772, 40.3146004823,
120.943801447],
[6.99469414131, 46.9678505732, 51.2011611144, 0, 93.9478658318],
[3773.94093782, 188.697046891, 943.485234456, 849.13671101, 377.394093782]],
['energiestadt','energiepolitik','energieversorgung','energietag',
'energiestrategie'])
import pandas as pd
import matplotlib.pyplot as plt
raw_data = {'Words': words,
'energie_energiestadt': word_freqs[0],
'energie_march2017': word_freqs[1],
'energie_smartcity': word_freqs[2]}
df = pd.DataFrame(raw_data, columns = ['Words', 'energie_energiestadt',
'energie_march2017', 'energie_smartcity'])
df
# Setting the positions and width for the bars
pos = list(range(len(df['energie_energiestadt'])))
width = 0.25
# Plotting the bars
fig, ax = plt.subplots(figsize=(10,5))
# Create a bar with energie_energiestadt data,
# in position pos,
plt.bar(pos,
#using df['energie_energiestadt'] data,
df['energie_energiestadt'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#EE3224',
# with label the first value in Words
label=df['Words'][0])
# Create a bar with energie_march2017 data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
#using df['energie_march2017'] data,
df['energie_march2017'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#F78F2E',
# with label the second value in Words
label=df['Words'][1])
# Create a bar with energie_smartcity data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos],
#using df['energie_smartcity'] data,
df['energie_smartcity'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#FFC222',
# with label the third value in Words
label=df['Words'][2], log=1)
# Set the y axis label
ax.set_ylabel('Frequency')
# Set the chart's title
ax.set_title('Frequency of words in different texts')
# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])
# Set the labels for the x ticks
ax.set_xticklabels(df['Words'])
# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(df['energie_energiestadt'] + df['energie_march2017'] +
df['energie_smartcity'])] )
# Adding the legend and showing the plot
plt.legend(['energie energiestadt', 'energie march2017', 'energie
smartcity'], loc='upper right')
plt.grid()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
y = [7637.78, 1938.77, 208.9, 40.31, 120.94, 6.99, 46.97,
51.2, 0.0, 93.95, 3773.94, 188.7, 943.49, 849.14, 377.39]
y = np.array(y)
fig, ax = plt.subplots()
ax.bar(range(len(y)), y)
ax.set_yscale("log")
ax.set_ylim((1e-1,None))
plt.show()