如何在 matplotlib 中格式化 y 轴,使其不是指数
How to format the y-axis in matplotlib so it's not an exponential
我正在创建图表,并得到下一个结果:
如何解决这个错误的编号和上面的 1e6
?
为什么会这样?
我尝试使用此 的解决方案,但没有成功。
我的代码:
import matplotlib.pyplot as plt
plt.title(f'TOP 10 videos by {choice}')
plt.xlabel('TOP places')
plt.ylabel(f'Number')
labels_top = ['Top10', 'Top9', 'Top8', 'Top7', 'Top6', 'Top5', 'Top4', 'Top3', 'Top2', 'Top1']
y_count = [50235, 90312, 150123, 250000, 290134, 370241, 385153, 768512, 955025, 1255531]
for graph in range(len(y_count[:10])):
plt.bar(labels_top.pop(), y_count.pop())
plt.gca().ticklabel_format(style='plain')
我收到这个错误:
Traceback (most recent call last):
File "/home/vitalii/PycharmProjects/mediagroupukraine_test_task/venv/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3130, in ticklabel_format
axis.major.formatter.set_scientific(is_sci_style)
AttributeError: 'StrCategoryFormatter' object has no attribute 'set_scientific'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 41, in <module>
start()
File "app.py", line 37, in start
app(channel_name, channel_id)
File "app.py", line 27, in app
menu_graphs()
File "app.py", line 25, in menu_graphs
show_statistic_graph(formatted_data_for_graphs, menu_choice)
File "/home/vitalii/PycharmProjects/mediagroupukraine_test_task/graphs.py", line 48, in show_statistic_graph
plt.gca().ticklabel_format(style='plain')
File "/home/vitalii/PycharmProjects/mediagroupukraine_test_task/venv/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3140, in ticklabel_format
raise AttributeError(
AttributeError: This method only works with the ScalarFormatter
您需要传递给 matplotlib.axes.Axes.ticklabel_format
您想要格式化的轴,在您的情况下只有 y
轴,所以:
plt.gca().ticklabel_format(axis='y', style='plain')
由于您没有将 axis
参数传递给 ticklabel_format
,它使用默认值,因此 both
。这样 matplotlib
也试图用 style = 'plain'
x 轴格式化,但这不可行,所以你得到 AttributeError
.
我正在创建图表,并得到下一个结果:
如何解决这个错误的编号和上面的 1e6
?
为什么会这样?
我尝试使用此
我的代码:
import matplotlib.pyplot as plt
plt.title(f'TOP 10 videos by {choice}')
plt.xlabel('TOP places')
plt.ylabel(f'Number')
labels_top = ['Top10', 'Top9', 'Top8', 'Top7', 'Top6', 'Top5', 'Top4', 'Top3', 'Top2', 'Top1']
y_count = [50235, 90312, 150123, 250000, 290134, 370241, 385153, 768512, 955025, 1255531]
for graph in range(len(y_count[:10])):
plt.bar(labels_top.pop(), y_count.pop())
plt.gca().ticklabel_format(style='plain')
我收到这个错误:
Traceback (most recent call last):
File "/home/vitalii/PycharmProjects/mediagroupukraine_test_task/venv/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3130, in ticklabel_format
axis.major.formatter.set_scientific(is_sci_style)
AttributeError: 'StrCategoryFormatter' object has no attribute 'set_scientific'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 41, in <module>
start()
File "app.py", line 37, in start
app(channel_name, channel_id)
File "app.py", line 27, in app
menu_graphs()
File "app.py", line 25, in menu_graphs
show_statistic_graph(formatted_data_for_graphs, menu_choice)
File "/home/vitalii/PycharmProjects/mediagroupukraine_test_task/graphs.py", line 48, in show_statistic_graph
plt.gca().ticklabel_format(style='plain')
File "/home/vitalii/PycharmProjects/mediagroupukraine_test_task/venv/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3140, in ticklabel_format
raise AttributeError(
AttributeError: This method only works with the ScalarFormatter
您需要传递给 matplotlib.axes.Axes.ticklabel_format
您想要格式化的轴,在您的情况下只有 y
轴,所以:
plt.gca().ticklabel_format(axis='y', style='plain')
由于您没有将 axis
参数传递给 ticklabel_format
,它使用默认值,因此 both
。这样 matplotlib
也试图用 style = 'plain'
x 轴格式化,但这不可行,所以你得到 AttributeError
.